Wanting2LearnMan
asked on
jquery selector question
I have a Html like so:
In my associated javascript file I do the following:
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)
<select id="myList" class="CustomSelect1">
<option value="0">MyOption1</option>
<option value="1">MyOption2</option>
</select>
In my associated javascript file I do the following:
var nic_list = $('#myList');
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)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bardobrave, where do you see .each loop?? I just assigned it to a variable
@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.
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.
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:
Is there a way I can get only the contents of one of the dropdown boxes??
Thanks for all your help
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK I changed it so each one has a unique id :)
So you can directly associate the result from your selector without having to iterate through it.