Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 the link for example :  
 
https://plnkr.co/edit/ndDtRL?p=preview


What I have tried:

$(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  do not want to set here 
});
}
Posted
Updated 12-Jun-19 19:59pm
Comments
Nirav Prabtani 13-Jun-19 1:33am    
//$('#sel').val('005'); /// I do not want to set here

then where do you want to set it ?
Anilananda 13-Jun-19 1:36am    
where i'm calling the function
===============

$(document).ready(function () {
fillDropdown();
$('#sel').val('005');// I want to set here this way
});
Nirav Prabtani 13-Jun-19 2:29am    
See the solution

1 solution

You can do something like this.

JavaScript
$(document).ready(function () {
fillDropdown('005');
});


JavaScript
function fillDropdown(valueData)
{
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>');
});
BindValue(valueData);
});
}

function BindValue(valueData){
 $('#sel').val(valueData);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900