Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find child by index jQuery

Tags:

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)?

like image 793
sergionni Avatar asked Oct 06 '09 14:10

sergionni


People also ask

How do you select an element by index?

The :eq() selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1). This is mostly used together with another selector to select a specifically indexed element in a group (like in the example above).

What does .EQ do in jQuery?

eq() method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set.

How can get Li tag value in jQuery?

$("#myid li"). click(function() { this.id = 'newId'; // longer method using . attr() $(this). attr('id', 'newId'); });


2 Answers

Try this: (.eq()):

selection.eq(1).text() 
like image 146
Tamas Czinege Avatar answered Sep 22 '22 01:09

Tamas Czinege


Try eq() instead of get():

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

You can also do it by selector:

$("div:eq(1)") 
like image 41
Lobstrosity Avatar answered Sep 19 '22 01:09

Lobstrosity