views:

493

answers:

4

My select list is called dropListBuilding. The folliowng code seems to not work:

 for (var i = 0; i < buildings.length; i++) {
     var val = buildings[i];
     var text = buildings[i];
     alert("value of builing at: " + i.toString() + " is: " + val);
     $("#dropListBuilding").addOption(val, text, false);
 }

This line dies: $("#dropListBuilding").addOption(val, text, false); What is the right to add items to the drop down in jquey. I have tested without that line and the buildings variable does have my data element.

+1  A: 
$('#dropListBuilding').append('<option>'+val+'</option>');
Soufiane Hassou
+2  A: 

Doing it this way has always worked for me, I hope this helps.

var ddl = $("#dropListBuilding");   
for (k = 0; k < buildings.length; k++)
   ddl.append("<option value='" + buildings[k]+ "'>" + buildings[k] + "</option>");
James
+1  A: 

It looks like you want this pluging as it follows your existing code, maybe the plug in js file got left out somewhere.

http://www.texotela.co.uk/code/jquery/select/

var myOptions = {
"Value 1" : "Text 1",
"Value 2" : "Text 2",
"Value 3" : "Text 3"
} 
$("#myselect2").addOption(myOptions, false); 
// use true if you want to select the added options » Run
Bob
+1  A: 

Your code fails because you are executing a method (addOption) on the jQuery object (and this object does not support the method)

You can use the standard Javascript function like this:

$("#dropListBuilding")[0].options.add( new Option("My Text","My Value") )
duckyflip
Can you point me to a reference for this method? I didn't think something like that existed, I thought the DOM method was `.options.add(`
Bob
Bob you are right, I've modified the code to use the normal JS way of adding an option
duckyflip