Mustapha George
Mustapha George

Reputation: 2527

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>

Upvotes: 38

Views: 93568

Answers (3)

James Hill
James Hill

Reputation: 61812

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

$("TheElementToInsert").insertAfter(".myClass tr:first");

Upvotes: 7

Rob Cowie
Rob Cowie

Reputation: 22619

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

$('.myclass tr:nth-child(2)')

Upvotes: 49

Seth
Seth

Reputation: 6260

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

This will grab the second one.

Upvotes: 73

Related Questions