Side navigation
#5416 closed bug (fixed)
Opened October 28, 2009 03:10PM UTC
Closed June 13, 2010 06:14PM UTC
find('something:first') can return multiple results
Reported by: | dwt | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.4 |
Component: | selector | Version: | 1.3.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Hi there,
maybe I understand something wrong but from the documentation it seems that the ':first' filter as well as all the ':nth(x)' filters should onyl ever return one element.
However they do not for me - and I've put a reduction of this for :first as an attachment to this.
Attachments (1)
Change History (4)
Changed October 28, 2009 03:58PM UTC by comment:1
Changed October 28, 2009 05:25PM UTC by comment:2
jQuery chained methods have an implicit loop in them to apply the operation to *each* element in the current jQuery object. The
.find()method is applied to the two DL elements in the object and in each one finds a DT element that is the first one. So it returns two DT elements, one for each DL element.
It seems like this is at least worth a mention in the page for
.find()because many people might think that
$("dl").find("dt:first")would be equivalent to
$("dl dt:first")but it's not in this case. The former returns the first DT descendant of each DL but the latter returns the first DT descandant of all DLs.
Note that if you just want the first element regardless of which element it came from, you could also use the
.eq()or
.slice()methods to get it.
Changed November 30, 2009 02:08PM UTC by comment:3
I just clarified this behavior in the docs:
http://docs.jquery.com/index.php?title=Traversing/find&diff=17199&oldid=15484
feel free to close this issue as fixed or wontfix.
Changed June 13, 2010 06:14PM UTC by comment:4
component: | unfiled → selector |
---|---|
resolution: | → fixed |
status: | new → closed |
comments by paul_irish on irc: like i said i think the comparison that aj screenshotted is the biggest wtf to be, but the difference of :first within a collection of elements vs a document-rooted selector is also worth noting. if anything i think JohnResig should clarify what we expect out of :first. maybe it is a bug, maybe the documentatio needs a bit more.
That screenshot: http://gyazo.com/b1cef7368a9f4c17ae56d597472def42.png
Same behaviour with this nightly: http://code.jquery.com/jquery-nightly.js at the time of posting this.