2

Please Help If anyone have solution.

I am using dependency Dropdown, Where on Selection of Dealer's Dropdown Customer's List is binded dynamically with Jquery.

While in Edit mode according to Dealer Value I'm calling a function in Document.Ready that Appends the Customer's list by getting data from Ajax call.

after this function executes I'm setting the selected value from options, but it is showing null/blank. with many solutions tried. I referred to many questions on this site but result is same. am i doing mistake anywhere?

<select id="ddlcustomerlstEdit" name="ddlcustomerlstEdit" class="form-control">
         <option value=""> -select Customer- </option>
</select>

Here is dynamic binding of List in function called within document.ready.

if (data != "Blank") {
      $("#ddlcustomerlstEdit").html("");
           var str = '<option value=""> -Select Customer- </option>';
           for (var i = 0; i < data.length; i++) {
               str += '<option value=' + data[i].CustomerID + '> ' + data[i].CustomerName + '</option>';
           }
                        $("#ddlcustomerlstEdit").append(str);
}

As soon as this function executes i'm setting the selected value.

var customerIdVal = $("#hdnCustomerID").val();
$('#ddlcustomerlstEdit option[value="' + customerIdVal+'"]').attr("selected", "true");
alert($("#ddlcustomerlstEdit").val());

$("#hdnCustomerID") this value is coming within model and the value it contains is present in <option> yet the alert coming is always null or blank.

Edit

enter image description here

7
  • Post the array that is inside data. Without that it is difficult to find what is missing. Commented Jan 2, 2018 at 7:59
  • Also where is your hdnCustomerID hidden element? Commented Jan 2, 2018 at 8:01
  • you can see the edit and hidded element contains the value user selected previously that is 2 in this case. i'm using that to compare within the option and then accordingly make that option as selected. @AnkitAgarwal Commented Jan 2, 2018 at 8:04
  • Check the answer and let me know if i understood you correct or not Commented Jan 2, 2018 at 8:06
  • yes you understood correct but the alert i'm getting is blank, and there's no difference in what you did. then where i'm mistaking? Commented Jan 2, 2018 at 8:14

1 Answer 1

1

This should be working.

$(document).ready(function() {
  var data = [{
    'CustomerID': '2',
    'CustomerName': 'Test55'
  }];
  if (data != "Blank") {
    $("#ddlcustomerlstEdit").html("");
    var str = '<option value=""> -Select Customer- </option>';
    for (var i = 0; i < data.length; i++) {
      str += '<option value=' + data[i].CustomerID + '> ' + data[i].CustomerName + '</option>';
    }
    $("#ddlcustomerlstEdit").append(str);

    var customerIdVal = $("#hdnCustomerID").val();
    $('#ddlcustomerlstEdit option[value="' + customerIdVal + '"]').attr("selected", "true");
    alert($("#ddlcustomerlstEdit").val());
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="ddlcustomerlstEdit" name="ddlcustomerlstEdit" class="form-control">
         <option value=""> -select Customer- </option>
</select>

<input type='hidden' value='2' id='hdnCustomerID' />

Sign up to request clarification or add additional context in comments.

5 Comments

can you please explain what is the difference in your Code and mine? because mine is still returning blank value. @AnkitAgarwal
Have you included JQuery? add debugger in the code and debug using chrome developer console. That should help you
Did you find what was the issue?
The Data you added in array was coming from an ajax call from me. and as i was trying to set the select value immediately after execution of function it was setting that first and then the options were appended as it was asynchronously running in background. I set async to false and then it went in flow as i required.
great. You can mark it green if it was helpful to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.