Bug Tracker

Opened 10 years ago

Closed 9 years ago

#5416 closed bug (fixed)

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)

find-child-returns-two.html (623 bytes) - added by dwt 10 years ago.

Download all attachments as: .zip

Change History (5)

Changed 10 years ago by dwt

Attachment: find-child-returns-two.html added

comment:1 Changed 10 years ago by dwt

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.

comment:2 Changed 10 years ago by dmethvin

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.

comment:3 Changed 10 years ago by paul.irish

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.

comment:4 Changed 9 years ago by dmethvin

Component: unfiledselector
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.