I want to populate a SELECT Dropdown List with JSON Data using JavaScript
JavaScript
success: function(data) {
var distList = data.d;
console.log(distList);
//consoleoutput = [{ "Districtname": "CENTRAL DELHI", "ID": 1 }, { "Districtname": "NORTH DELHI", "ID": 2 }, { "Districtname": "NORTH EAST DELHI", "ID": 3 }, { "Districtname": "NORTH WEST DELHI", "ID": 4 }, { "Districtname": "SOUTH DELHI", "ID": 5 }, { "Districtname": "SOUTH EAST DELHI", "ID": 6 }, { "Districtname": "SOUTH WEST DELHI", "ID": 7 }, { "Districtname": "WEST DELHI", "ID": 8 }]
var s = 'Please Select a Distic';
for (var i = 0; i ' + distList[i].Districtname + '';
}
$("#dist").html(s);
}i tried another approach
JavaScript
$.each(distList, function (key, value) {
$('#dist').append('' + value + '');
});but
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"Districtname":"CENTRAL.................... ,{"Districtname":"WEST DELHI","ID":8}]
plz help


Muzaffar Ur RahmanRank #608 · 3.9K reputationPosted Nov 23, 2023, 12:39 AM EST
Hi, Could you please have look on this articel.
https://checkedexception.com/blog/populate-a-select-dropdown-list-with-json
Khushi Soni Rajpoot5 reputationPosted Nov 23, 2023, 12:55 AM EST
i solve this
let distList = JSON.parse(data.d); //method 1 //var s = 'Please Select a District'; //for (var i = 0; i ' + distList[i].Districtname + ''; //} //$("#dist").html(s); //method 2 $("#dist").html(""); $.each(distList, function (key, value) { $('#dist').append('' + distList[key].Districtname + ''); });Amit MohantyRank #112 · 20K reputationPosted Nov 23, 2023, 1:19 AM EST
You can also try this to set value as well as text.
success: function(data) { var distList = data.d; var option = 'Please Select a District'; $.each(distList, function (index, district) { option += '' + district.Districtname + ''; }); $("#dist").html(option); }