jQuery 1.7.1 – I have a check box column and would like to get the index of selected boxes as an array or iterate through each checkbox – check whether its selected or not, if selected get the index.
HTML
<tr>
<td><input type="checkbox" class="it" name="it"></td>
</tr>
<tr>
<td><input type="checkbox" class="it" name="it"></td>
</tr>
If you use the
.each()method jQuery will pass the index as an argument to the callback function you supply. (You’ll probably see a lot of code around that uses.each()without parameters on the callback, but that’s just because you often don’t need to know the index – but it’s there for when you do.) Also when calling your function jQuery setsthisto the current element:Noting that the index is zero-based, and in case it’s not obvious it is the index within the set of elements that matched the selector you supplied (not within all elements in the document).
Note also that above I’m selecting elements by class, but you could select by the name attribute: