In jQuery, $("...").get(3)
returns the 3rd DOM element. What is the function to return the 3rd jQuery element?
The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.
jQuery :nth-child() Selector The :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent. Tip: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.
Using jQuery :nth-child Selector You have put the position of an element as its argument which is 2 as you want to select the second li element. If you want to get the exact element, you have to specify the index value of the item. A list element starts with an index 0.
The eq() method returns an element with a specific index number of the selected elements. The index numbers start at 0, so the first element will have the index number 0 (not 1).
You can use the :eq selector, for example:
$("td:eq(2)").css("color", "red"); // gets the third td element
Or the eq(int) function:
$("td").eq(2).css("color", "red");
Also, remember that the indexes are zero-based.
Why not browse the (short) selectors page first?
Here it is: the :eq()
operator. It is used just like get()
, but it returns the jQuery object.
Or you can use .eq()
function too.
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