How can I get a list of all values in select box?
example:
<select name="sel1" id="sel1">
<option value="001">option 001</option>
<option value="002">option 002</option>
<option value="003">option 003</option>
<option value="004">option 004</option>
</select>
values:
let selectElement = document.querySelectorAll('[name=sel1]');
let optionValues = [...selectElement[0].options].map(o => o.value)
option text
let selectElement = document.querySelectorAll('[name=sel1]');
let optionNames = [...selectElement[0].options].map(o => o.text)
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 "