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>
$('.myclass tr').eq(1)
This will grab the second one.
Use the nth-child selector. See http://api.jquery.com/nth-child-selector/
$('.myclass tr:nth-child(2)')
Use the :first
selector in combination with the insertAfter()
function:
$("TheElementToInsert").insertAfter(".myClass tr:first");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With