sergionni
sergionni

Reputation: 13510

Find child by index jQuery

jQuery can return last or first child,it works ok.

But I need to get second child.

This construction (get child by index) doesn't work,when get its text:

child.parent().parent().children().get(1).text()

So, how can i find non-last and non-first child (e.g. second)?

Upvotes: 22

Views: 33512

Answers (3)

Florian Mertens
Florian Mertens

Reputation: 2448

In one of my sites, I have:

$('#tr_' + intID).find("td").eq(3).html("Hello there!");

Essentially, this will get all the TD elements from a table TR with id='tr_123'.

eq(3) then gets the (0-indexed!) fourth cell of that TR, and changes its HTML contents to Hello there!.

Upvotes: 1

Lobstrosity
Lobstrosity

Reputation: 3936

Try eq() instead of get():

child.parent().parent().children().eq(1).text()

You can also do it by selector:

$("div:eq(1)")

Upvotes: 8

Tamas Czinege
Tamas Czinege

Reputation: 121294

Try this: (.eq()):

selection.eq(1).text()

Upvotes: 40

Related Questions