#7556 closed bug (invalid)
:eq selector doesn't select the right element
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | high | Milestone: | |
Component: | selector | Version: | 1.4.4 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
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 (8)
comment:1 Changed 13 years ago by
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:2 follow-up: 3 Changed 13 years ago by
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 Changed 13 years ago by
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 13 years ago by
Component: | unfiled → selector |
---|---|
Priority: | undecided → high |
Resolution: | worksforme |
Status: | closed → reopened |
Looks like a bug in Sizzle. Maybe uniqueSort
should be applied earlier
comment:6 Changed 13 years ago by
Status: | reopened → open |
---|
comment:7 Changed 12 years ago by
Keywords: | needsdocs added |
---|---|
Resolution: | → invalid |
Status: | open → closed |
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 12 years ago by
Keywords: | needsdocs removed |
---|
First attempt at condensing down the docs update required for this: http://api.jquery.com/eq-selector/
Please double check your selectors
http://jsfiddle.net/rwaldron/yfr8Z/3/