How to get nth jQuery element
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 have control over the query which builds the jQuery object, use :eq()
$("div:eq(2)")
If you don't have control over it (for example, it's being passed from another function or something), then use .eq()
var $thirdElement = $jqObj.eq(2);
Or if you want a section of them (say, the third, fourth and fifth elements), use .slice()
var $third4th5thElements = $jqObj.slice(2, 5);
I think you can use this
$("ul li:nth-child(2)").append("<span> - 2nd!</span>");
It finds the second li in each matched ul and notes it.