views:

60

answers:

2

Hey guys i bet this is an easy question but cant seem to find any help online, i have a select List box and i would like to get each list text and split them up into an array of how big the list box is without the user selecting the value. Right now my list box makes random values so the list can go up to 100

Example

<select id="selectBox">

   <option value="1">Select One</option>    <--- I want just the text to input into array
   <option vaue="2">Select Two</option>

</select>
+3  A: 

There is already that functionality in the HTML Dom

Here is a referencing link for the HTML Dom SelectElement Options http://www.w3schools.com/jsref/coll_select_options.asp

var myoptions = $("#mySelectElementid")[0].options;

from here you can do the following

myoptions.length;
myoptions[0].value;
myoptions[0].text;
John Hartsock
+3  A: 

Use the DOM to get at them:

var options = document.getElementById("selectBox");
var optArray = [];
for (var i = 0; i < options.length; i++) {
    optArray.push(options[i].text);
}
// now optArray has all the text elements from the option tags.
DaveS