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>
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>
Share
Improve this question
edited May 20, 2010 at 19:04
Silent
asked May 20, 2010 at 18:50
SilentSilent
6811 gold badge16 silver badges34 bronze badges
1
- Could you give an example with some code? I'm not sure what you mean when you say "list text" and "how big the list box is". Split them up by what? – Kerry Jones Commented May 20, 2010 at 18:54
2 Answers
Reset to default 4There is already that functionality in the HTML Dom
Here is a referencing link for the HTML Dom SelectElement Options http://www.w3schools./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;
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.