I have markup
<table>
<tr id="1">
<td colspan="4">
<p class="que">
1. Who are you?</p>
</td>
</tr>
<tr class="ans">
<td>
<input type="checkbox" />Student
</td>
<td>
<input type="checkbox" checked="true" />Developer
</td>
<td>
<input type="checkbox" />Other
</td>
<td>
<input type="text" />
</td>
</tr>
</table>
Here I want to get the index of the particular td which has its checkbox checked. For example here it should be 1. But I m getting 0 eachtime which seems like the index of the row.
Here is the jquery code I have used.
var answers = $('table tr.ans');
$.each(answers, function () {
var answer = $(this).find("input[type='checkbox']:checked").index();
alert(answer);
});
and here is the fiddle
How do I get the index of the particular td?
Thanks
You can do it with
You simply need to navigate from the checkbox back up to the
<td>, at which point stright calling.indexdoes the trick:See it in action.
Since you are doing this inside a loop, a more proper fit would be