Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:34:36+00:00 2026-05-30T17:34:36+00:00

I have an interesting problem that i can’t seem to figure out. I am

  • 0

I have an interesting problem that i can’t seem to figure out. I am using JSON to grab some data from a datasource and display the information in a form.

What is not happening properly is that i can’t seem to get jQuery to add an attribute of “selected” to the proper option.

My problem is that if I use some static data via a variable it works fine. Here is my code that does what I want it to do: Here’s a quick JS Fiddle of the below code

  <ul>
            <li class="item">
                <label>Name</label>
                <input type="text" id="txtName"></li>
            <li class="item">
                <label>Description</label>
                <input type="text" id="txtDescription"></li>
            <li class="item">
                <label> Unit</label>
              <select id="ddlUnit" >
                    <option value="blank" >Original Stuff</option>
                    <option value="#">#</option>
                    <option value="$">$</option>
                    <option value="%">%</option>
                    <option value="Days">Days</option>
                    <option value="Hrs">Hrs</option>
                    <option value="Mins">Mins</option>
                    <option value="Mths">Mths</option>
                    <option value="Qtrs">Qtrs</option>
                    <option value="Rate">Rate</option>
                    <option value="Wks">Wks</option>
                    <option value="Yrs">Yrs</option>
                </select>
            </li>
        </ul>




    <script type="text/javascript">
         $(document).ready(function() {
                $("#txtName").val('Testing Name!');
                $("#txtDescription").val('Testing The Description!');
                var selectedKPI = '#';
                $("#ddlUnit").find("option[value='"+selectedKPI+"']").attr('selected','selected');
         });
    </script>

That works. It adds “Selected” attribute to the proper option.

However! When I try to grab the data from a data source it doesn’t work. What’s the deal?!

Heres’ my code that does not work. This code loads the data into the “input” fields fine. I can even get the proper data to come across when I use an alert. It just doesn’t add “selected” attribute to the proper option.

 <ul>
            <li class="item">
                <label>
                    Name</label>
                <input type="text" id='txtName' /></li>
            <li class="item">
                <label>
                    Description</label>
                <input type="text" id='txtDescription' /></li>
            <li class="item">
                <label>
                    Unit</label>
                <select id='ddlUnit'>
                    <option value="blank">Select a KPI</option>
                    <option value="#">#</option>
                    <option value="$">$</option>
                    <option value="%">%</option>
                    <option value="Days">Days</option>
                    <option value="Hrs">Hrs</option>
                    <option value="Mins">Mins</option>
                    <option value="Mths">Mths</option>
                    <option value="Qtrs">Qtrs</option>
                    <option value="Rate">Rate</option>
                    <option value="Wks">Wks</option>
                    <option value="Yrs">Yrs</option>
                </select>
            </li>
        </ul>
    </div>
    <script type="text/javascript">
         function LoadKpiCallback(result) {
                var kpi = result.d;              
                $("#txtName").val(kpi.KpiName);
                $("#txtDescription").val(kpi.KpiDescription);
                var selectedKPI = kpi.KpiUnit ;
                alert(selectedKPI + "2");
                $("#ddlUnit").find("option[value='"+selectedKPI+"']").attr("selected","selected");
         }

        function SaveSuccess() {
           //close the window                 
            CloseKendoWindow();            
            //refresh the parent grid
            LoadKpiList(); 
        }

        function AjaxError(){
           alert('There was a problem saving your KPI!');
        }

        function SaveKPI(){
        //load kpi object from controls
        var kpi = {KPI: {
                    KpiID :<%= Me.KpiID %>,
                    KpiName: $("#txtName").val(),
                    KpiDescription :$("#txtDescription").val(),
                    KpiUnit:$("#ddlUnit option:selected").text()
                   }};
                   alert(kendo.stringify(kpi));

                //use ajax to save
                 $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async:true,
                    cache: false,                
                    data: kendo.stringify(kpi),
                    url: "Services/RockService.svc/SaveKPI",
                    success: SaveSuccess,
                    error: AjaxError                    
                });    
            }

        function LoadKpi() {
            $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async:true,
                    cache: false,                
                    data: kendo.stringify({KpiID : <%= Me.KpiID %>}),
                    url: "Services/RockService.svc/GetKPI",
                    success: LoadKpiCallback,
                    error: function (xhr) {
                        alert("Bad");
                        alert(xhr.responseText);
                    }
                });    
            }

            LoadKpi();

    </script>

What on earth could be the problem? Why would it work with local data, and fail with remote data, but only on the select??

Thanks in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:34:37+00:00Added an answer on May 30, 2026 at 5:34 pm

    $.trim() should remove any additional spaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting problem to solve here that may require some creative direction.
I have an interesting problem. I'd like to create a generic class that can
I have an interesting SQL problem that I need help with. Here is the
I have an interesting genetics problem that I would like to solve in native
I have an interesting problem. The basis of the problem is that my last
We have an interesting problem with WCF binding and streaming transfer mode that we
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I have some interesting problem for an hour.. In my flex project, all width
I have an interesting problem when using partial page update in asp.net with scriptmanager
I have an interesting problem being reported to me from an android application I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.