jQuery eq() method can be used to select an element by specifying its index position. The index position of the group element like a table starts at 0. It can be negative or a position to specify.
Syntax
The syntax of this method is given below:-
Description of the Parameters
The description of these parameters is given below.
Parameter Name | Description |
---|---|
selector | Specify the element to select for the specified index position. You can use the element tag name, class name, or id to select. It is a required field. |
index | You have to specify the index position of the element to select. |
jQuery eq() Method Return Specified Index Element
If you want to select the item of the group element, you have to specify the index position as given below. The method can be helpful to locate the exact element you want to select.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> $(document).ready(function(){ $('.mybtn').click(function(){ $("ul li").eq(1).css("color", "yellow"); }); }); </script> <ul> <li>First list element</li> <li>Second list element</li> <li>Third element</li> <li>Forth element</li> </ul> <button type="button" class="mybtn">Click to Select</button> |
Output
- First list element
- Second list element
- Third element
- Forth element
When you click the button given above, it selects the element for which you have specified the index position. After you click the button, the output shows that you have selected the second element.
You may also like to read
I hope you like this tutorial on jQuery eq() method. If you have any queries regarding the tutorial, please comment below.
References