JQuery - Iterating a Collection
I am new to JQuery. I have a select html element. I am trying to figure out how to iterate over the options in a select element. I know how to do it with regular javascript as shown here:
for (i=0; i<mySelect.options.length; i++)
alert(mySelect.options[i].value);
However, I am trying to learn more about JQuery. Can anyone show me a better way to iterate through a collection using JQuery?
thanks
a source to share
First: IMHO, it's great that you learn the non-jQuery way. Make sure you don't slip into the idea that jQuery is better for everything.
To the problem: If you have a DOM reference mySelect
to select
, then you can get the jQuery object option
using $(mySelect).find("option")
. The usual way to scroll through them is with a jQuery method each
and an (anonymous) function:
$(mySelect).find("option").each(function() {
alert(this.value);
})
a source to share