Avatar of Wanting2LearnMan
Wanting2LearnManFlag 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
SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Bardobrave
Bardobrave
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Bardobrave
Bardobrave
Flag of Spain image

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.
Avatar of YZlat
YZlat
Flag of United States of America image

Bardobrave, where do you see .each loop?? I just assigned it to a variable
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

@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.
Avatar of 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
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Wanting2LearnMan

ASKER

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

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo