Anilananda Chakrabortty

Anilananda Chakrabortty

  • 1.5k
  • 388
  • 27.4k

Bind a dropdown list and set a default value in jQuery

Jun 12 2019 12:41 PM
HI i'm facing a problem i want to bind the data to dropdown in document.ready function then i want to set a value to the dropdown to highlite the selected value.
 
Data is binding but im not able to set a value as a selected value.
 
here is my code...
 
$(document).ready(function () {
fillDropdown();
$('#sel').val('005');// I want to set here this way
});
function fillDropdown()
{
var url = "sample.json";
$.getJSON(url, function (data) {
$.each(data, function (index, value) {
// APPEND OR INSERT DATA TO SELECT ELEMENT.
$('#sel').append('<option value="' + value.ID + '">' + value.Name + '</option>');
});
//$('#sel').val('005'); /// I dnt want to set here
});
}
 
Here is the link for example :
https://plnkr.co/edit/ndDtRL?p=preview
Thanks

Answers (6)