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)?
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).
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.
$("#myid li"). click(function() { this.id = 'newId'; // longer method using . attr() $(this). attr('id', 'newId'); });
Try this: (.eq()
):
selection.eq(1).text()
Try eq()
instead of get()
:
child.parent().parent().children().eq(1).text()
You can also do it by selector:
$("div:eq(1)")
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