Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - select the second row in a table?

How do I target the second <tr> in a table using jQuery? Do I use .closest or nth-child?

// something like this..
var MyLocation = $('.myclass').closest('tr').after('tr');   // fixed


<table class ='myclass'>
<tr>
  <td></td>
</tr>
   <!-- put some thing here -->
<tr>

</tr>
like image 467
Mustapha George Avatar asked Nov 13 '11 23:11

Mustapha George


3 Answers

$('.myclass tr').eq(1)

This will grab the second one.

like image 146
Seth Avatar answered Nov 01 '22 14:11

Seth


Use the nth-child selector. See http://api.jquery.com/nth-child-selector/

$('.myclass tr:nth-child(2)')
like image 49
Rob Cowie Avatar answered Nov 01 '22 15:11

Rob Cowie


Use the :first selector in combination with the insertAfter() function:

$("TheElementToInsert").insertAfter(".myClass tr:first");
like image 7
James Hill Avatar answered Nov 01 '22 15:11

James Hill