Skip to content

updating Understand-index.md #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions page/using-jquery-core/understanding-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ In the first example, `.index()` gives the zero-based index of `#foo1` within it

Potential confusion comes from the other examples of `.index()` in the above code. When `.index()` is called on a jQuery object that contains more than one element, it does not calculate the index of the first element as might be expected, but instead calculates the index of the last element. This is equivalent to always calling `$jqObject.last().index();`.


As of **jQuery version 1.9.0b1** the `.index()` functions uses `.first()` instead of `.last()`,
so on versions greater than **1.9.0b1** the above code would produce the following result.

```
var $foo = $( "#foo1" );

console.log( "Index: " + $foo.index() ); // 1

var $listItem = $( "li" );

// This implicitly calls .first()
console.log( "Index: " + $listItem.index() ); // 1
console.log( "Index: " + $listItem.first().index() ); // 1

var $div = $( "div" );

// This implicitly calls .first()
console.log( "Index: " + $div.index() ); // 0
console.log( "Index: " + $div.first().index() ); // 0
```


## `.index()` with a String Argument

```
Expand Down