Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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:
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 9 years ago by Rick Waldron

Resolution: worksforme
Status: newclosed

Please double check your selectors

http://jsfiddle.net/rwaldron/yfr8Z/3/

comment:2 Changed 9 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 9 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 9 years ago by jitter

Component: unfiledselector
Priority: undecidedhigh
Resolution: worksforme
Status: closedreopened

Looks like a bug in Sizzle. Maybe uniqueSort should be applied earlier

simplified test case

Last edited 9 years ago by jitter (previous) (diff)

comment:5 Changed 9 years ago by dmethvin

#4641 is a duplicate of this ticket.

comment:6 Changed 9 years ago by snover

Status: reopenedopen

comment:7 Changed 9 years ago by danheberden

Keywords: needsdocs added
Resolution: invalid
Status: openclosed

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 9 years ago by addyosmani

Keywords: needsdocs removed

First attempt at condensing down the docs update required for this: http://api.jquery.com/eq-selector/

Note: See TracTickets for help on using tickets.