.index()Returns: Integer
Description: Search for a given element from among the matched elements.
-
version added: 1.4.index()
- This signature does not accept any arguments.
-
version added: 1.4.index( selector )
-
selectorType: 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 first element within the jQuery object 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:
1
2
3
4
5
|
|
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:
1
2
|
|
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:
1
2
|
|
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.
1
2
|
|
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.
1
2
|
|
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:
1
|
|
Again, we get back the zero-based position of the list item:
Index: 1
Examples:
On click, returns the index (zero-based) of that div in the page.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
|
Demo:
Returns the index for the element with ID bar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
|
Demo:
Returns the index for the first item in the jQuery collection.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
|
Demo:
Returns the index for the element with ID bar in relation to all <li> elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
|
Demo:
Returns the index for the element with ID bar in relation to its siblings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
|
Demo:
Returns -1, as there is no element with ID foobar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
|