Hello-World
Hello-World

Reputation: 9555

index of tr with tds only --- relative to click

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

Answers (2)

Vinayak Phal
Vinayak Phal

Reputation: 8919

Try this...

$('td').click(function(){
   if ( ! $(this).closest('table').find('th').length ) {
       var s = $(this).closest('tr').index();
       alert(parseInt(s) + 1);
   }
});​

JS FIDDLE LINK

Upvotes: 1

Mangala Edirisinghe
Mangala Edirisinghe

Reputation: 1111

Your given code is working properly. see this jsfiddle.

Upvotes: 0

Related Questions