jQuery API

:eq() Selector

eq selector

version added: 1.0jQuery(':eq(index)')

  • index
    Zero-based index of the element to match.

Description: Select the element at index n within the matched set.

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.

Unlike the .eq(index) method, the :eq(index) selector does not accept a negative value for index. For example, while $('li').eq(-1) selects the last li element, $('li:eq(-1)') selects nothing.

Additional Notes:

  • Because :eq() is a jQuery extension and not part of the CSS specification, queries using :eq() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").eq(index) instead.

Examples:

Example: Finds the third td.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <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>
<script>$("td:eq(2)").css("color", "red");</script>

</body>
</html>

Demo:

Example: 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.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <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>

<script>
// 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" );
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :eq() Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :eq() Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://profiles.yahoo.com/u/QO4LGCNAYSQXODHIZCQJG4TBNI techbp

    Hello! Does the selector eq accepts variables instead of values?
    ex code:

    var n = 5;
    $(‘li:eq(n)’).animate({ ‘left’ : ’40%’}, ‘slow’);

    I can’t get it work whenever I put a variable inside the eq selector. But if I replaced it with an integer value it does good… I want to use a variable instead of a value because I need it within my code….

    Thank you in advance

  • http://www.learningjquery.com/ Karl Swedberg

    $(‘li:eq(‘ + n + ‘)’)

  • Anh

    Take the first example of :nth-child() and substitute with :eq(1), and the result is not the same, i.e. only the 2nd of the first is modified. What is the reason that :eq() works differently from :nth-child() in this regard?

  • Tilendor

    $(“li”).eq(n).animate(…)

  • http://www.learningjquery.com/ Karl Swedberg

    The second paragraph of the description says it all:

    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.

  • Anh

    I meant to point out that the :nth-child() example shows 2 ‘s being modified vs 1 when using :eq().

  • http://www.learningjquery.com/ Karl Swedberg

    ok. That is because the two selectors are fundamentally different. The :nth-child selector gets every element within the jQuery set that is the nth-child of its parent. The :eq selector only ever gets one element within the jQuery set, the “nth” (0-indexed) element.

  • MasterJarek

    Greetings,

    is there a way to match a series of numbers, something like eq(4n+1), e.g. 1,5,9,13,17,… ?

  • MasterJarek

    Ah nth-child(4n+1) works ;)

  • Asha

    How to loop through the jQuery(‘:eq(index)’)

  • http://www.learningjquery.com/ Karl Swedberg

    There is nothing to loop through. The :eq() selector always matches a single element. Maybe you're looking for .each()?

  • Justin

    There's a caveat everybody should be aware of: If you've used .eq() you probably know it supports negative indexes. While you might assume the pseudo-selector was implemented with the same method call, it appears it wasn't, as it doesn't support negative indexes and will only return an empty result set.

  • http://twitter.com/nyuszika7h Nyuszika7H

    I think you're looking for :lt() and/or :gt().

  • http://twitter.com/nyuszika7h Nyuszika7H

    I think you're looking for :lt() and/or :gt().