jQuery API

.eq()

.eq( index ) Returns: jQuery

Description: Reduce the set of matched elements to the one at the specified index.

  • version added: 1.1.2.eq( index )

    indexAn integer indicating the 0-based position of the element.

  • version added: 1.4.eq( -index )

    -indexAn integer indicating the position of the element, counting backwards from the last element in the set.

Given a jQuery object that represents a set of DOM elements, the .eq() method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set.

Consider a page with a simple list on it:

  <ul>
    <li>list item 1</li>
    <li>list item 2</li>
    <li>list item 3</li>
    <li>list item 4</li>
    <li>list item 5</li>
  </ul>

We can apply this method to the set of list items:

  $('li').eq(2).css('background-color', 'red');

The result of this call is a red background for item 3. Note that the supplied index is zero-based, and refers to the position of the element within the jQuery object, not within the DOM tree.

Providing a negative number indicates a position starting from the end of the set, rather than the beginning. For example:

  $('li').eq(-2).css('background-color', 'red');

This time list item 4 is turned red, since it is two from the end of the set.

If an element cannot be found at the specified zero-based index, the method constructs a new jQuery object with an empty set and a length property of 0.

  $('li').eq(5).css('background-color', 'red');

Here, none of the list items is turned red, since .eq(5) indicates the sixth of five list items.

Example:

Turn the div with index 2 blue by adding an appropriate class.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { width:60px; height:60px; margin:10px; float:left;
        border:2px solid blue; }
  .blue { background:blue; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div></div>
  <div></div>
  <div></div>

  <div></div>
  <div></div>
  <div></div>
<script>

    $("body").find("div").eq(2).addClass("blue");
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .eq() 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()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • David

    Let’s say you are using a click event and want to get the item’s current index number, would one simply use eq() with no parameter?

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

      One would simply use the .index() method.

      • Jules

        won’t that return the index of the element in DOM. What if you have the code: $(‘.subtotal’).change(function() { }) how would you detect the how-manyth match you’re at? I think .eq() without parameters should do this, but it doesn’t…

        • Jules

          Never mind. $(‘.subtotal’).change(function() { $(‘.subtotal’).index($(this)) }); does the trick

  • Anonymous

    it’s be cool, as i can get range of elements, like as
    $(‘ul li:eq( 1, -1 )’ )

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

      You can with the .slice() method: http://api.jquery.com/slice

      • http://3r1c.myopenid.com/ Eric

        What if you wanted the first, second and fifth elements? Not just multiple consecutive elements?

        • Anonymous

          Use the multiple selector property, although this isn’t in traversing it’s still the same principle. Using the example above:
          $(“div:eq(0), div:eq(1), div:eq(4)”).addClass(“blue”);

          Or you can use .end():
          $(“div”).eq(0).addClass(“blue”).end()
          .eq(1).addClass(“blue”).end()
          .eq(4).addClass(“blue”).end();

      • http://www.google.com/profiles/praveenius Praveen

        You saved me tons of time. Thanks!!

  • Gene

    Shouldn’t the last example be adding the blue class to the third box since the index is set to .eq(2)?

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

      Are you using Firefox with Firebug enabled? If so, disable Firebug and see if that fixes the problem for you.

      • Valmonde

        Indeed. By disabling Firebug on this page, the 3rd box will be blue (the correct answer) else the 2nd box will be blue.
        1. Could you explain why?
        2. Are there any more conflicts (between FF and Firebug) we should know about?
        As a Javascript programmer I use Firebug all the time while testing the scripts.

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

          Firebug inserts a div element into the DOM, in between the head and body elements (if they exist). If you want to exclude the Firebug div from a collection of all divs, you could find divs within 'body': $('body').find('div')

  • Moss

    I found that using this method instead of the inline string :eq() is faster.

    For example:

    $(“table”).find(“tr”).eq(0)

    is faster than

    $(“table”).find(“tr:eq(0)”)

  • jccc

    I don't know if this has been fixed in versions more recent than 1.2.6, but eq(1) consistently returns multiple elements (perhaps all elements after index #1, as my array has only three).

    When I switch to using the string “:eq(1)” in my selectors it works correctly.

  • Anon

    Please specify what happens when you input an index that is non-existent.

  • Milobt

    What if you want to select the index's that are not eq().
    E.g.

    $('.feedContainer:eq(:not('+Index+')) '+divID+')').slideUp('fast');

    As the opposite to:

    $('.feedContainer:eq('+Index+') '+divID).slideUp('fast');

    I am very new to jquery and I have a row of divs that will expand and contract, but I need a line that will cause the siblings to contract when a different one is expanded.

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

      If they're truly siblings, you can use the .siblings() method to select them.

  • http://www.google.com/profiles/praveenius Praveen

    You saved me tons of time. Thanks!!