Avatar of Wanting2LearnMan
Wanting2LearnMan
Flag for United Kingdom of Great Britain and Northern Ireland asked on

jquery selector question

I have a Html like so:

 <select id="myList" class="CustomSelect1">    
     <option value="0">MyOption1</option>	
     <option value="1">MyOption2</option>		
  </select>

Open in new window


In my associated javascript file I do the following:
var nic_list = $('#myList');

Open in new window


In my Firefox debugger I can see that when I expand out nic_list and see the option values MyOption1 and MyOption2.

How do I properly get these values and store them in an array?? (There can be any number of entries in the dropdown box)
jQueryJavaScriptAJAX

Avatar of undefined
Last Comment
Wanting2LearnMan

8/22/2022 - Mon
SOLUTION
YZlat

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
YZlat

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Bardobrave

If I'm not mistaken you don't need the .each loop, as when a jQuery selector matches more than one element he already returns an array with all the matched elements.

So you can directly associate the result from your selector without having to iterate through it.
YZlat

Bardobrave, where do you see .each loop?? I just assigned it to a variable
Chris Stanyon

@Bardobrave - you are :)

Try your code - it just grabs the option text from the first option in the list. The question was specifically about how to store all options in an array, so you need to loop through the collection and add the text for each option to the array.

@YZlat - your code grabs the text for the selected option - not all of them.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Wanting2LearnMan

ASKER
I used this:
var myArray = new Array();
$('#myList option').each(function() {
    myArray.push( $(this).text());
});

Thanks,

It does the trick.
But now since I have several rows in a html table all with a dropdown box, I end up with an array as follows:
myArray["MyOption1", "MyOption2", "MyOption1", "MyOption2", "MyOption1",
"MyOption2"]

Is there a way I can get only the contents of one of the dropdown boxes??

Thanks for all your help
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Wanting2LearnMan

ASKER
OK I changed it so each one has a unique id :)