Skip to content

Add note on bug with .index() before 1.9 #582

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 1 commit into from

Conversation

arthurvr
Copy link
Member

@arthurvr arthurvr commented Dec 8, 2014

Added a little note on the bug with .index() in jquery < 1.9.

The examples were fixed with 54946db, where .first() was added without including any explanation about it. This results in even more confusion.

In general this needs to be known by people, so makes sense to include it.

Would fix #276.

@gnarf
Copy link
Member

gnarf commented Dec 23, 2014

I think we can reword that and the sentence before it to not be all like "bug in old version" more like:

Note: Before jQuery 1.9, .index() only worked reliably on a single element, which is why we've used .first() on each of our examples. In jQuery 1.9+ this can be ignored, as the API was updated to define that it operates on the first element only.

I'm still not sold, but I think we can word it a little cleaner that doesn't make it sound too bad?

@arthurvr
Copy link
Member Author

@gnarf Agreed. Updated the PR.

@@ -38,7 +38,8 @@ console.log( "Index: " + div.first().index() ); // 0

In the first example, `.index()` gives the zero-based index of `#foo1` within its parent. Since `#foo1` is the second child of its parent, `index()` returns 1.

When `.index()` is called on a jQuery object that contains more than one element, it calculates the index of the first element.

__Note__: Before jQuery 1.9, .index() only worked reliably on a single element, which is why we've used .first() on each of our examples. In jQuery 1.9+ this can be ignored, as the API was updated to define that it operates on the first element only.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: backtick the .index and .first()

also - not to fix now, but should we maybe make more of these methods in learn articles links to api?

@arthurvr
Copy link
Member Author

@gnarf Updated PR once again.

also - not to fix now, but should we maybe make more of these methods in learn articles links to api?

We should hard-bake it in to beginners that everything can be find in api docs, so why not?

@gnarf gnarf closed this in 9ec218b Dec 23, 2014
arthurvr added a commit to arthurvr/learn.jquery.com that referenced this pull request Jan 4, 2015
Talk about why we use `.first()` in the examples.

Closes jquerygh-582
Fixes jquerygh-276
Krinkle pushed a commit that referenced this pull request Apr 9, 2024
Talk about why we use `.first()` in the examples.

Closes gh-582
Fixes gh-276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

article on index() - index() with no arg now implicitly calls first() not last from jQuery 1.91
2 participants