Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nth jQuery element

Tags:

jquery

In jQuery, $("...").get(3) returns the 3rd DOM element. What is the function to return the 3rd jQuery element?

like image 369
Sam Lee Avatar asked Sep 18 '09 06:09

Sam Lee


People also ask

How do you find the nth 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.

What is nth jQuery?

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.

How can I get second child in jQuery?

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.

What is the use of EQ in jQuery?

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


2 Answers

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.

like image 184
Christian C. Salvadó Avatar answered Dec 04 '22 13:12

Christian C. Salvadó


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.

like image 32
Dykam Avatar answered Dec 04 '22 13:12

Dykam