English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The eq() method returns the element with the specific index number of the selected elements.
The index always starts from 0, so the first number will have an index of 0 (not1).
$(selector).eq(index)
Returns the third paragraph (index number2):
$("button").click(function(){ $("p").eq(2).css("background-color", "red"); });Test to see‹/›
Returns the second list item (index number1):
$("button").click(function(){ $("li").eq(1).css("background-color", "red"); });Test to see‹/›
Provide negative numbers to indicate positions starting from the end instead of the beginning:
$("button").click(function(){ $("li").eq(-2).css("background-color", "red"); });Test to see‹/›
By adding the appropriate class, the index of2the span's class name becomes blue:
$("button").click(function(){ $("body").find("span").eq(2).addClass("blue"); });Test to see‹/›
Parameter | Description |
---|---|
index | An integer indicating the zero-based position of the element Note:Using negative numbers will start indexing from the end of the selected element instead of the beginning |