jQuery API

.index()

.index( ) Returns: Number

Description: Search for a given element from among the matched elements.

  • version added: 1.4.index()

  • version added: 1.4.index( selector )

    selectorA selector representing a jQuery collection in which to look for an element.

  • version added: 1.0.index( element )

    elementThe DOM element or first element within the jQuery object to look for.

Return Values

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

If .index() is called on a collection of elements and a DOM element or jQuery object is passed in, .index() returns an integer indicating the position of the passed element relative to the original collection.

If a selector string is passed as an argument, .index() returns an integer indicating the position of the original element relative to the elements matched by the selector. If the element is not found, .index() will return -1.

Detail

The complementary operation to .get(), which accepts an index and returns a DOM node, .index() can take a DOM node and returns an index. Suppose we have a simple unordered list on the page:

<ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>

If we retrieve one of the three list items (for example, through a DOM function or as the context to an event handler), .index() can search for this list item within the set of matched elements:

var listItem = document.getElementById('bar');
alert('Index: ' + $('li').index(listItem));
We get back the zero-based position of the list item:

Index: 1

Similarly, if we retrieve a jQuery object consisting of one of the three list items, .index() will search for that list item:

var listItem = $('#bar');
alert('Index: ' + $('li').index(listItem));

We get back the zero-based position of the list item:

Index: 1

Note that if the jQuery collection used as the .index() method's argument contains more than one element, the first element within the matched set of elements will be used.

var listItems = $('li:gt(0)');
alert('Index: ' + $('li').index(listItems));

We get back the zero-based position of the first list item within the matched set:

Index: 1

If we use a string as the .index() method's argument, it is interpreted as a jQuery selector string. The first element among the object's matched elements which also matches this selector is located.

var listItem = $('#bar');
alert('Index: ' + listItem.index('li'));

We get back the zero-based position of the list item:

Index: 1

If we omit the argument, .index() will return the position of the first element within the set of matched elements in relation to its siblings:

alert('Index: ' + $('#bar').index();

Again, we get back the zero-based position of the list item:

Index: 1

Examples:

Example: On click, returns the index (based zero) of that div in the page.

<!DOCTYPE html>
<html>
<head>
  <style>
div { background:yellow; margin:5px; }
span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <span>Click a div!</span>
<div>First div</div>
<div>Second div</div>
<div>Third div</div>
<script>
$("div").click(function () {
  // this is the dom element clicked
  var index = $("div").index(this);
  $("span").text("That was div index #" + index);
});
</script>

</body>
</html>

Demo:

Example: Returns the index for the element with ID bar.

<!DOCTYPE html>
<html>
<head>
  <style>div { font-weight: bold; color: #090; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>
<div></div>
<script>var listItem = $('#bar');
    $('div').html( 'Index: ' + $('li').index(listItem) );</script>

</body>
</html>

Demo:

Example: Returns the index for the first item in the jQuery collection.

<!DOCTYPE html>
<html>
<head>
  <style>div { font-weight: bold; color: #090; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>
<div></div>
<script>var listItems = $('li:gt(0)');
$('div').html( 'Index: ' + $('li').index(listItems) );
</script>

</body>
</html>

Demo:

Example: Returns the index for the element with ID bar in relation to all <li> elements.

<!DOCTYPE html>
<html>
<head>
  <style>div { font-weight: bold; color: #090; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>
<div></div>
<script>$('div').html('Index: ' +  $('#bar').index('li') );</script>

</body>
</html>

Demo:

Example: Returns the index for the element with ID bar in relation to its siblings.

<!DOCTYPE html>
<html>
<head>
  <style>div { font-weight: bold; color: #090; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>
<div></div>
<script>var barIndex = $('#bar').index();
$('div').html( 'Index: ' +  barIndex );</script>

</body>
</html>

Demo:

Example: Returns -1, as there is no element with ID foobar.

<!DOCTYPE html>
<html>
<head>
  <style>div { font-weight: bold; color: #090; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>
<div></div>
<script>var foobar = $("li").index( $('#foobar') );
$('div').html('Index: ' + foobar);</script>

</body>
</html>

Demo:

Support and Contributions

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

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

  • Aleksandr Motsjonov

    .index() without parameters is very-very cool. It’s so usefull! Thank you.

  • dude

    How exactly did this change from jquery 1.3.2?

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

      Only the third signature was available in 1.3.2

  • Van

    This is very cool, but what if you wrapped your <div>s in other elements? For example, the first option above, “On click, returns the index (based zero) of that div in the page.”, how could you return the index if there were more <div>s and other elements? i.e.
    <div class=”divclass” id=”div-id”>
    <div>First div</div>
    <div>Second div</div>
    <div>Third div</div>
    </div>

    Or having other nested elements before and after on page.

  • http://surroundview.net/ Jon Kirkman

    As of 1.4.2 .index() without parameters seems to return ridiculous values on IE6.
    In my case it wasn’t a big deal as I could easily reformat my statement such as .index(‘.myclass’).

  • Mark Ballenger

    Call me crazy, but this simply wasn’t working for me. I was trying to get the index of a checked radio by doing

    $(‘#the_parent_div’).find(‘input:radio:checked’).index()

    and even though the jquery array contained the selected value when debugging, the returned value was always -1. wtf? Am I missing something? Well regardless I turned to this little script which did what I was needing so I added the function as a plugin.

    http://snipplr.com/view/15788/get-index-of-the-current-element/

    Anyone else have this problem?

    • teajeans

      Having the same problem; I have jQuery 1.2.6. Calling .index() with no argument seems to always return -1.

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

        That's because the ability to use .index() with no arguments wasn't introduced until version 1.4 (as noted above).

        • teajeans

          oh…I'm blind. thanks :)

          • http://al3xa.myopenid.com/ aL3xa

            Or you can use:
            $(“#the_parent_div”).index(“input:radio:checked”);

  • Vitaly

    How can i get valid index if some of element is invisible (display none) ?

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

    That's because the ability to use .index() with no arguments wasn't introduced until version 1.4 (as noted above).

  • teajeans

    oh…I'm blind. thanks :)

  • Guest

    ok so maybe I'm missing something..

    var ttn = this.tagName;
    var tta = $(this).parent().index(ttn);

    tta = -1 ???

    HOW is that possible? the parent of this tag, HAS to have a tag named the same somewhere within it, because it was just from its child of that given tag name. I dont get it.

    • Danny

      .parent moves to the parent of this; this isn't part of its own parent.
      You want to do
      tta = $(this).parent().children().index(ttn);