question is in the subject
views:
39answers:
3
Q:
how do i get the row (and its index) in an html table from a button click inside a cell in that row.
A:
You have to go back up through the DOM until you find the <tr> element.
function onclick_handler( event ) {
var element = event.target;
while( element.tagName != 'TR' && element.parentNode ) {
element = element.parentNode;
}
return element.rowIndex;
}
sakabako
2009-12-18 04:59:56
A:
onclick="alert(this.parentNode.rowIndex);"
Unless you have a tbody type tag, then use parentNode twice. Or in jquery:
onclick="alert($(this).closest('tr').attr('rowIndex'));"
Yuriy Faktorovich
2009-12-18 05:01:08
+1
A:
$("yourtableselector td").click(function() {
alert($(this).parents("tr").get(0).rowIndex);
});
jitter
2009-12-18 14:10:15