Skip to main content

Bug Tracker

Side navigation

#7556 closed bug (invalid)

Opened November 18, 2010 11:45PM UTC

Closed March 31, 2011 03:59AM UTC

Last modified May 02, 2011 05:33AM UTC

:eq selector doesn't select the right element

Reported by: Eric Lebetsamer <elebet@gmail.com> 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/

Attachments (0)
Change History (8)

Changed November 18, 2010 11:51PM UTC by rwaldron comment:1

resolution: → worksforme
status: newclosed

Please double check your selectors

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

Changed November 18, 2010 11:56PM UTC by Eric Lebetsamer <elebet@gmail.com> comment:2

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?

Changed November 19, 2010 12:06AM UTC by Eric Lebetsamer <elebet@gmail.com> comment:3

Replying to [comment:2 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/

Changed November 19, 2010 01:01AM UTC by jitter comment:4

_comment0: Looks like a bug in Sizzle. Maybe `uniqueSort` should be applied earlier1290181260303904
component: unfiledselector
priority: undecidedhigh
resolution: worksforme
status: closedreopened

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

simplified test case

Changed November 19, 2010 02:47AM UTC by dmethvin comment:5

#4641 is a duplicate of this ticket.

Changed November 22, 2010 07:13AM UTC by snover comment:6

status: reopenedopen

Changed March 31, 2011 03:59AM UTC by danheberden comment:7

keywords: → needsdocs
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.

Changed May 02, 2011 05:33AM UTC by addyosmani comment:8

keywords: needsdocs

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