I have a row in an html-table that contains only images. (this happens to be the first row too). Those images are wired for a click event too. While trying to handle the click event I can find out its parent (i.e. <td>
element). But I want to know its relative ordinal in the table row (<tr
>).
views:
115answers:
3
A:
A call to .closest()
within your click event handler might help you.
$(this).closest('tr');
Reference: .closest()
jAndy
2010-06-09 11:03:06
+1
A:
You can find the ordinal with the index()
function:
$('td img').click(function() {
var ordinal = $(this).closest('tr').children().index($(this).parent());
// ...
});
reko_t
2010-06-09 11:05:42
A:
The number of elements before the current element will be its index in the DOM relative to the parent. You can use .prevAll()
and get the length of the resulting set:
$('td').click(function(){
alert($(this).prevAll().length);
});
gnarf
2010-06-09 11:10:58