Johannes Lantz
Johannes Lantz

Reputation: 55

JQuery targeting child

I want to target the second <tr> and the second <td> tag using JQuery. I know I could give the <td> tag a class and just target that one but I want to do it this way. This is what I've got so far:

$('#playlist tr:nth-child(2)'); 
<table id="playlist">
    <tr class="bold">
        <td>ID:</td>
        <td>Track-Name:</td>
        <td>Artist:</td>
        <td>Duration:</td>
    </tr>
    <tr>
        <td class="idD">1</td>
        <td id="first" song="Weown.mp3" cover="cover.jpg" artist="The Wanted">We Own The Night</td>
        <td>The Wanted</td>
        <td class="idD">3:25</td>
    </tr>
</table>

Upvotes: 0

Views: 46

Answers (3)

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

Above answers are correct but you can also use eq()

$("#playlist tr").eq(2).find('td').eq(2);

check the demo on jsFiddle

Upvotes: 0

Manwal
Manwal

Reputation: 23816

Consider following:

$('#playlist tr:nth-child(2) td:nth-child(2)')
//or
$('#playlist tr:eq(1) td:eq(1)')

See it in action

Details: :nth-child(), :eq()

Upvotes: 1

Curtis
Curtis

Reputation: 103348

This would be:

$('#playlist tr:nth-child(2) td:nth-child(2)'); 

http://jsfiddle.net/kzwbkfz3/

Upvotes: 2

Related Questions