Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am stumped. I have a form with a dropdown list, and I would like to grab a list of all the values in the list. I pulled the below example from w3 schools (yes, I know it's unreliable, but other solutions on stack overflow seem to be very similar to this). It was not working for me, and I tried plugging it into jsfiddle, but no luck.

HTML: <form>Select your favorite fruit:     <select id="mySelect">         <option value="a">Apple</option>         <option value="o">Orange</option>         <option value="p">Pineapple</option>         <option value="b">Banana</option>     </select> </form> <button type="button" onclick="displayResult()">Display text of all options</button> 

javascript:

function displayResult() {     var x = document.getElementById("mySelect");     var txt = "All options: ";     var i;     for (i = 0; i < x.length; i++) {         txt = txt + "\n" + x.options[i].value;     }     alert(txt); } 

Not working on jsfiddle: http://jsfiddle.net/WfBRr/1/

However, it works at their site: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_option_text2

Any ideas on how to solve this?

like image 865
dan.m.kumar Avatar asked Aug 07 '13 20:08

dan.m.kumar


People also ask

How get all values from Dropdownlist?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

How do you find the value of the select list?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).

What is the return type of get all selected options?

selectedOptions returns a list of selected items. Specifically, it returns a read-only HTMLCollection containing HTMLOptionElements. ... is spread syntax. It expands the HTMLCollection 's elements.

How do I iterate a dropdown in jQuery?

Simple jQuery code snippet to loop select box options (drop down boxes) in a form to get the values and text from each option. Useful for manipulating values in form select boxes. $('#select > option'). each(function() { alert($(this).


2 Answers

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/

like image 56
Abraham P Avatar answered Sep 30 '22 13:09

Abraham P


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 "

like image 26
Nir Alfasi Avatar answered Sep 30 '22 11:09

Nir Alfasi