jquery selector question

Wanting2LearnMan
Wanting2LearnMan used Ask the Experts™
on
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)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
myarray = $('#myList').val();
sorry, try

myarray = $('#myList').text();
Try this:

$("#myList > option").html();
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
This should do the trick:
var myArray = new Array();
$('#myList option').each(function() {
    myArray.push( $(this).text());
});

Open in new window

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.
Bardobrave, where do you see .each loop?? I just assigned it to a variable
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
@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.

Author

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The code I posted will grab the options from a dropdown with an ID of myList. As IDs are supposed to be unique, you should only have 1 dropdown with that ID on your Page. If not, you need to change your HTML.

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial