.index() returns the zero-based position of an element (since jQuery 1.0). With no argument it reports where the first matched element sits among its siblings. With a selector, DOM element, or jQuery object it searches that set instead. If nothing matches, it returns -1.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("#bar").index() // position among siblings → 1
$("li").index(element) // where element sits in the $("li") set
$("li").index($("#baz")) // jQuery object: first element is searched
$("#bar").index("li") // selector: where #bar sits in $("li")
// Not found → -1
$("li").index(document.body) // -1
$("#missing").index() // -1
// Returns a Number (not a jQuery object)
Call
What it does
.index()
Position of the first matched element among its siblings
.index(element)
Position of a DOM element within the current set
.index(jQuery)
Position of the jQuery object's first element within the current set
.index(selector)
Position of the current set's first element within $(selector)
Not found
Returns -1
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
selector
A selector string. The first element of the current set is searched for inside the elements matched by this selector.
element
A DOM element (or a jQuery object). It is searched for inside the current set; for a jQuery object, its first element is used.
(none)
No argument: the position of the first element among all of its siblings (children of the same parent).
Return Value
Return Value
Description
Number
A zero-based integer position.
-1
Returned when the element is not found, or when the set is empty or has no parent to count siblings in.
jQuery Examples
Example 1 — Position Among Siblings
Call .index() with no argument to find where an item sits in its list.
Reverse roles. With a selector, the argument defines the set to search.
Position.#bar is second in li but first in li.on.
Not found.#baz is not in li.on, so -1.
Frequently Asked Questions
.index() returns the zero-based position of an element. With no argument it returns the first matched element's position among its siblings; with a selector, DOM element, or jQuery object it searches that set instead. Available since jQuery 1.0.
-1. This happens when the element is not in the searched set, when the set is empty, or when the element has no parent. Always compare against -1 rather than treating the result as truthy, because 0 is a valid position.
With a selector, the current set's first element is searched inside $(selector). With an element or jQuery object, that element is searched inside the current set. The roles of the two sets are reversed.
With no argument it counts all preceding siblings of the first matched element, regardless of what the selector matched. Pass a selector such as .index("span") to count only within a filtered set.
.eq(n) goes from a position to an element; .index() goes from an element to its position. They are inverse operations for the same set.
No. It returns a number, not a jQuery object, so use it at the end of a chain or store the result in a variable.