In jQuery, $("...").get(3) returns the 3rd DOM element. What is the function to return the 3rd jQuery element?
+9
A:
You can use the :eq selector, for example:
$("td:eq(2)").css("color", "red"); // gets the third td element
Or the Traversing/eq function:
$("td").eq(2).css("color", "red");
Also, remember that the indexes are zero-based.
CMS
2009-09-18 06:56:08
+2
A:
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);
nickf
2009-09-18 07:00:13
A:
I think you can use this
$("ul li:nth-child(2)").append(" - 2nd!");
It finds the second li in each matched ul and notes it.
Joenas Ejes
2010-05-13 05:57:51