Ticket #7556 (closed bug: invalid)
:eq selector doesn't select the right element
| Reported by: | Eric Lebetsamer <elebet@…> | Owned by: | |
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | selector | Version: | 1.4.4 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
I'm trying to select a link in a list (or list of lists) based on it's index, but the :eq(index) selector doesn't select the right link. If I select the list item based on the index, I get the right list element, but the using the link element doesn't get the right one.
Test case is located here: http://jsfiddle.net/yfr8Z/
Change History
comment:1 Changed 3 years ago by rwaldron
- Status changed from new to closed
- Resolution set to worksforme
comment:2 follow-up: ↓ 3 Changed 3 years ago by Eric Lebetsamer <elebet@…>
Your test case doesn't contain a list with sub lists though. Can I not use the eq selector to get items from the sub lists too?
comment:3 in reply to: ↑ 2 Changed 3 years ago by Eric Lebetsamer <elebet@…>
Replying to Eric Lebetsamer <elebet@…>:
Your test case doesn't contain a list with sub lists though. Can I not use the eq selector to get items from the sub lists too?
I have updated my test case to make it a bit more simple and to show that "$('a:eq(6)')" does not return the same as "$('a').eq(6)". Shouldn't these return the same thing? http://jsfiddle.net/yfr8Z/4/
comment:4 Changed 3 years ago by jitter
- Priority changed from undecided to high
- Resolution worksforme deleted
- Status changed from closed to reopened
- Component changed from unfiled to selector
Looks like a bug in Sizzle. Maybe uniqueSort should be applied earlier
comment:7 Changed 2 years ago by danheberden
- Keywords needsdocs added
- Status changed from open to closed
- Resolution set to invalid
I think you're confusing how :eq and .eq() should operate.
$( 'ul.nav a:eq(6)' ) is essentially saying "get the 7th a element in ul.nav". But as sizzle goes through the dom, your first ul.nav element doesn't have 7 a elements, it only has 4 - thus you end up with the last one it could find.
$( 'ul.nav a' ) - now this gets all the a elements inside of a ul.nav ( 11 of them ) and .eq(6) gets the 7th one from that set.
Basically, it's an order of operations thing. .eq() is acting on the resulting set of objects, where as :eq() is working on that particular element. This is why $( 'a:eq(6)' ) and $( 'a' ).eq(6) both return the same element.
Also, eq is designed to retreive ONE element. So using it as a filter, for example, like $( 'ul.nav li a:eq(1)' ) and expecting it to get the 2nd a for every li isn't the expected behaviour. For that kind of operation, you'd want nth-child()
I'm marking it as needsdocs so that we can see about trying to address this in the documentation.
Hope this helps.
comment:8 Changed 2 years ago by addyosmani
- Keywords needsdocs removed
First attempt at condensing down the docs update required for this: http://api.jquery.com/eq-selector/
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Please double check your selectors
http://jsfiddle.net/rwaldron/yfr8Z/3/