:eq() Selector

Select the element at index n within the matched set.

.eq(index)

index NumberZero-based index of the element to match.

.eq(indexFromEnd)

indexFromEnd IntegerZero-based index of the element to match, counting backwards from the last element.

As of jQuery 3.4, the :eq pseudo-class is deprecated. Remove it from your selectors and filter the results later using .eq().

The index-related selectors (:eq(), :lt(), :gt(), :even, :odd) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set. For example, if elements are first selected with a class selector (.myclass) and four elements are returned, these elements are given indices 0 through 3 for the purposes of these selectors.

Note that since JavaScript arrays use 0-based indexing, these selectors reflect that fact. This is why $( ".myclass:eq(1)" ) selects the second element in the document with the class myclass, rather than the first. In contrast, :nth-child(n) uses 1-based indexing to conform to the CSS specification.

Prior to jQuery 1.8, the :eq(index) selector did not accept a negative value for index (though the .eq(index) method did).

Find the third td.

JS
<table border="1">
  <tr>
    <td>TD #0</td>
    <td>TD #1</td>
    <td>TD #2</td>
  </tr>
  <tr>
    <td>TD #3</td>
    <td>TD #4</td>
    <td>TD #5</td>
  </tr>
  <tr>
    <td>TD #6</td>
    <td>TD #7</td>
    <td>TD #8</td>
  </tr>
</table>
HTML
$("td:eq( 2 )").css("color", "red");
DEMO

Apply three different styles to list items to demonstrate that :eq() is designed to select a single element while :nth-child() or :eq() within a looping construct such as .each() can select multiple elements.

JS
<ul class="nav">
  <li>List 1, item 1</li>
  <li>List 1, item 2</li>
  <li>List 1, item 3</li>
</ul>
<ul class="nav">
  <li>List 2, item 1</li>
  <li>List 2, item 2</li>
  <li>List 2, item 3</li>
</ul>
HTML
// Applies yellow background color to a single <li>
$("ul.nav li:eq(1)").css("backgroundColor", "#ff0");

// Applies italics to text of the second <li> within each <ul class="nav">
$("ul.nav").each(function (index) {
  $(this).find("li:eq(1)").css("fontStyle", "italic");
});

// Applies red text color to descendants of <ul class="nav">
// for each <li> that is the second child of its parent
$("ul.nav li:nth-child(2)").css("color", "red");
DEMO

Add a class to List 2, item 2 by targeting the second to last <li>

JS
<ul class="nav">
  <li>List 1, item 1</li>
  <li>List 1, item 2</li>
  <li>List 1, item 3</li>
</ul>
<ul class="nav">
  <li>List 2, item 1</li>
  <li>List 2, item 2</li>
  <li>List 2, item 3</li>
</ul>
CSS
.foo {
  color: blue;
  background-color: yellow;
}
HTML
$("li:eq(-2)").addClass("foo");
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)