How can I get a list of all values in select box? How can I get a list of all values in select box? javascript javascript

How can I get a list of all values in select box?


You had two problems:

1) The order in which you included the HTML. Try changing the dropdown from "onLoad" to "no wrap - head" in the JavaScript settings of your fiddle.

2) Your function prints the values. What you're actually after is the text

x.options[i].text; instead of x.options[i].value;

http://jsfiddle.net/WfBRr/5/


Change:

x.length

to:

x.options.length

Link to fiddle

And I agree with Abraham - you might want to use text instead of value

Update
The reason your fiddle didn't work was because you chose the option: "onLoad" instead of: "No wrap - in "


As per the DOM structure you can use below code:

var x = document.getElementById('mySelect');     var txt = "";     var val = "";     for (var i = 0; i < x.length; i++) {         txt +=x[i].text + ",";         val +=x[i].value + ",";      }