Reputation: 9555
How do I get the index of the <tr>
in the table but only if it has <td>s
and not <th>s
If I use a click event below it will alert the index of the with td
and th
but I only want <tr>
with <td>s
thanks
$('td').click(function(){
var s = $(this).parents('table tr:has(td)').index();////// returns
alert(s);
});
<table>
<tr>
<th><th><th><th><th><th><th><th><th><th>
</tr>
<tr>
<td><td><td><td><td><td><td><td><td><td>
</tr>
<tr>
<td><td><td><td><td><td><td><td><td><td>
</tr>
<tr>
<td><td><td><td><td><td><td><td><td><td>
</tr>
<tr>
<td><td><td><td><td><td><td><td><td><td>
</tr>
</table>
Upvotes: 0
Views: 67
Reputation: 8919
Try this...
$('td').click(function(){
if ( ! $(this).closest('table').find('th').length ) {
var s = $(this).closest('tr').index();
alert(parseInt(s) + 1);
}
});
Upvotes: 1
Reputation: 1111
Your given code is working properly. see this jsfiddle.
Upvotes: 0