{"name":"index","type":"method","title":".index()","deprecated":null,"removed":null,"desc":"Search for a given element from among the matched elements.","categories":["miscellaneous/dom-element-methods","version/1.0","version/1.4"],"entries":[{"return":"Number","signatures":[{"added":"1.4"},{"added":"1.4","argument":{"desc":"A selector representing a jQuery collection in which to look for an element.","name":"selector","type":"Selector"}},{"added":"1.0","argument":{"desc":"The DOM element or first element within the jQuery object to look for.","type":[{"name":"Element"},{"name":"jQuery"}],"name":"element"}}],"examples":[{"desc":"On click, returns the index (zero-based) of that div in the page.","code":"\n$( \"div\" ).click(function() {\n // `this` is the DOM element that was clicked\n var index = $( \"div\" ).index( this );\n $( \"span\" ).text( \"That was div index #\" + index );\n});\n","css":"\n div {\n background: yellow;\n margin: 5px;\n }\n span {\n color: red;\n }\n","html":"\nClick a div!\n
First div
\n
Second div
\n
Third div
\n"},{"desc":"Returns the index for the element with ID bar.","css":"\n div {\n font-weight: bold;\n color: #090;\n }\n","code":"\nvar listItem = $( \"#bar\" );\n$( \"div\" ).html( \"Index: \" + $( \"li\" ).index( listItem ) );\n","html":"\n\n
\n"},{"desc":"Returns the index for the first item in the jQuery collection.","css":"\n div {\n font-weight: bold;\n color: #090;\n }\n","code":"\nvar listItems = $( \"li:gt(0)\" );\n$( \"div\" ).html( \"Index: \" + $( \"li\" ).index( listItems ) );\n","html":"\n\n
\n"},{"desc":"Returns the index for the element with ID bar in relation to all
  • elements.","css":"\n div {\n font-weight: bold;\n color: #090;\n }\n","code":"\n$( \"div\" ).html( \"Index: \" + $( \"#bar\" ).index( \"li\" ) );\n","html":"\n\n
    \n"},{"desc":"Returns the index for the element with ID bar in relation to its siblings.","css":"\n div {\n font-weight: bold;\n color: #090;\n }\n","code":"\nvar barIndex = $( \"#bar\" ).index();\n$( \"div\" ).html( \"Index: \" + barIndex );\n","html":"\n\n
    \n"},{"desc":"Returns -1, as there is no element with ID foobar.","css":"\n div {\n font-weight: bold;\n color: #090;\n }\n","code":"\nvar foobar = $( \"li\" ).index( $( \"#foobar\" ) );\n$( \"div\" ).html( \"Index: \" + foobar );\n","html":"\n\n
    \n"}],"longdesc":"\n

    Return Values

    \n

    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.

    \n

    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.

    \n

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

    \n

    Detail

    \n

    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:

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

    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:

    \n
    \nvar listItem = document.getElementById( \"bar\" );\nalert( \"Index: \" + $( \"li\" ).index( listItem ) );\n    
    \n

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

    \n

    \n Index: 1\n

    \n

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

    \n
    \nvar listItem = $( \"#bar\" );\nalert( \"Index: \" + $( \"li\" ).index( listItem ) );\n    
    \n

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

    \n

    \n Index: 1\n

    \n

    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.

    \n
    \nvar listItems = $( \"li:gt(0)\" );\nalert( \"Index: \" + $( \"li\" ).index( listItems ) );\n    
    \n

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

    \n

    \n Index: 1\n

    \n

    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.

    \n
    \nvar listItem = $( \"#bar\" );\nalert( \"Index: \" + listItem.index( \"li\" ) );\n    
    \n

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

    \n

    \n Index: 1\n

    \n

    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:

    \n
    \nalert( \"Index: \" + $( \"#bar\" ).index() );\n    
    \n

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

    \n

    \n Index: 1\n

    \n "}]}