My select list is called dropListBuilding
. The following code does not seem to work:
for (var i = 0; i < buildings.length; i++) {
var v
This is working fine, try out this.
var ob = $("#myListBox");
for (var i = 0; i < buildings.length; i++) {
var val = buildings[i];
var text = buildings[i];
ob.prepend("<option value="+ val +">" + text + "</option>");
}
If you do not want to rely on the 3.5 kB plugin for jQuery or do not want to construct the HTML string while escapping reserved HTML characters, here is a simple way that works:
function addOptionToSelectBox(selectBox, optionId, optionText, selectIt)
{
var option = document.createElement("option");
option.value = optionId;
option.text = optionText;
selectBox.options[selectBox.options.length] = option;
if (selectIt) {
option.selected = true;
}
}
var selectBox = $('#veryImportantSelectBox')[0];
addOptionToSelectBox(selectBox, "ID1", "Option 1", true);
$('#dropListBuilding').append('<option>'+val+'</option>');