How to get an element with a specific index in jQuery With Example

In this post, we are going to discuss how to get an element with a specific index. jQuery API provides eq() method for this operation.

jQuery eq() method

The jQuery eq() method is used to get an element with a specific index of the selected HTML element. You can give either positive or negative integer value as index. The index of the first element of the matched element is 0. If we use selector.eq(-1), it will return the last element and selector.eq(0) will return the first element in the matched set of elements.

Here is the general syntax for using jQuery eq() method:

index could be any positive or negative integer.

jQuery eq() example

Following example demonstrates the jQuery eq() method usage.

In this example, we can see the use of jQuery eq() method. The $("div").eq(1) will return the second div element and changes the color to yellow. Similarly the elements are searched when index is negative, so -6 will return the first div element in this html page. Below image shows the output produced by above HTML page.


That’s all for now. We will discuss some more traversing techniques in the coming posts.

By admin

Leave a Reply