jQuery, get ID of each element in a class using .each? jQuery, get ID of each element in a class using .each? jquery jquery

jQuery, get ID of each element in a class using .each?


Try this, replacing .myClassName with the actual name of the class (but keep the period at the beginning).

$('.myClassName').each(function() {    alert( this.id );});

So if the class is "test", you'd do $('.test').each(func....

This is the specific form of .each() that iterates over a jQuery object.

The form you were using iterates over any type of collection. So you were essentially iterating over an array of characters t,e,s,t.

Using that form of $.each(), you would need to do it like this:

$.each($('.myClassName'), function() {    alert( this.id );});

...which will have the same result as the example above.


patrick dw's answer is right on.

For kicks and giggles I thought I would post a simple way to return an array of all the IDs.

var arrayOfIds = $.map($(".myClassName"), function(n, i){  return n.id;});alert(arrayOfIds);