1

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>
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? Commented May 20, 2010 at 18:54

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.