eye eye - 4 months ago 8
jQuery Question

jQuery - find all IDs in a class

Let's say i have a html table like this:

<table>
<tr id="a" class="red"><td>test</td></tr>
<tr id="b" class="red"><td>test</td></tr>
<tr id="c" class="red"><td>test</td></tr>
<tr id="d" class="red"><td>test</td></tr>
<tr id="e" class="green"><td>test</td></tr>
<tr id="f" class="blue"><td>test</td></tr>
</table>


How can i loop through/get all ids of class "red" using jQuery?

Answer

Use .each()

var idArray = [];
$('.red').each(function () {
    idArray.push(this.id);
});