The index method
The index method returns the index of an element in a jQuery set (zero-based indexing).
Syntax
Returns the position number of the first selected element, relative to adjacent elements on the page (adjacent elements are elements that have a common parent):
.index();
Returns the position number of the first selected element among the elements that satisfy the selector. If such an element is not found, the method returns -1:
.index(selector);
The parameter can also be a DOM element or a jQuery object (if it contains multiple elements, the method will be executed for the first one):
.index(element);
Example
Let's print the number li with #test:
<ol>
<li>text</li>
<li id="test">text</li>
<li>text</li>
</ol>
let num = $('li').index('#test');
alert(num);
See also
-
property
length,
which gets the number of elements in the set