Bug Tracker

Modify

Ticket #5416 (closed bug: fixed)

Opened 4 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

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

Change History

Changed 4 years ago by dwt

comment:1 Changed 4 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 4 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 3 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 3 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to fixed
  • Component changed from unfiled to selector

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.