jQuery .index() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.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)
CallWhat 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 foundReturns -1

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
selectorA selector string. The first element of the current set is searched for inside the elements matched by this selector.
elementA 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 ValueDescription
NumberA zero-based integer position.
-1Returned 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.

jQuery
console.log($("#foo").index());  // 0
console.log($("#bar").index());  // 1
console.log($("#baz").index());  // 2
Try It Yourself

How It Works

  1. Select. Each selector matches one li in the same list.
  2. Count. .index() counts preceding siblings of the first match.
  3. Zero-based. The first item is 0, the third is 2.

Example 2 — Search Within the Set by Element or jQuery

Pass a DOM element or jQuery object to find its position inside the current set.

jQuery
var $li = $("li");

console.log($li.index(document.getElementById("bar")));  // 1
console.log($li.index($("#baz")));                       // 2
console.log($li.index(document.body));                   // -1
Try It Yourself

How It Works

  1. Current set. $li holds foo, bar, and baz.
  2. Search. The element or the jQuery object's first node is looked up in that set.
  3. Missing. document.body is not in the set, so the result is -1.

Example 3 — Search Within a Selector

Pass a selector to measure the position inside the elements it matches.

jQuery
// <li id="bar" class="on"> is the only .on item

console.log($("#bar").index("li"));     // 1
console.log($("#bar").index("li.on"));  // 0
console.log($("#baz").index("li.on"));  // -1
Try It Yourself

How It Works

  1. Reverse roles. With a selector, the argument defines the set to search.
  2. Position. #bar is second in li but first in li.on.
  3. 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.

Next: .is() Method

Test whether any matched element matches a selector, element, or function.

.is() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful