Skip to main content

Bug Tracker

Side navigation

#12009 closed bug (fixed)

Opened July 03, 2012 08:28PM UTC

Closed October 30, 2012 05:36PM UTC

jQueryObject.find(element) corrupts the stack

Reported by: Owned by: mikesherov
Priority: low Milestone: 1.9
Component: traversing Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:

Version: 1.7.2

Browser: Chrome 21 dev, Firefox 14

OS: Windows 7 64-bit


Expectation: I should be able to chain $(blah).find(element).andSelf().filter(element) and effectively check if blah or its descendants contain element.

Actually happens: $(blah).find(element).andSelf().filter(element).length is always 1 because .find(element) corrupts the stack and .andSelf().filter(element) is always a jQuery object only wrapping element

Attachments (0)
Change History (11)

Changed July 03, 2012 08:38PM UTC by anonymous comment:1

I fixed some logic and removed redundant CSS.

Changed July 12, 2012 01:39AM UTC by dmethvin comment:2

resolution: → invalid
status: newclosed

The "problem line" boils down to this:


That will either be 0 or 1 elements. Since the event has bubbled to this it would seem like **must** be either this or one of its descendants so the result will always be So I have no idea what you're trying to do here, or what "corrupts the stack" means. Please ask for help on the forum.

Changed July 12, 2012 02:55AM UTC by comment:3

Don't confuse yourself; this has nothing to do with the bubbling. See the jsFiddle below with an even simpler example (sorry for not reducing it even further in the first place).

The problem is the implementation of jQuery.fn.find, particularly this section:

if (typeof selector !== "string") {
    return jQuery(selector).filter(function () {
        for (i = 0, l = self.length; i < l; i++) {
            if (jQuery.contains(self[i], this)) {
                return true;

Internally jQuery keeps a context stack. Every traversal should push the previous context on the stack. That's what makes jQuery.fn.andSelf() work.

Because $(this).find(someElement) hits that block of code above, the stack becomes corrupted. That is, it effectively starts all over.

Thus, the next operation, .andSelf() is going to make return a jQuery object only wrapping someElement. Then when .filter(someElement) gets executed, it's obviously always going to return a jQuery object with a length of 1.

Here's a simpler demonstration of the problem:

It all has to do with WHAT you pass into jQuery.fn.find().

Changed July 12, 2012 03:04AM UTC by comment:4 made the output more intuitive...

Changed July 12, 2012 03:17AM UTC by dmethvin comment:5

resolution: invalid
status: closedreopened

Okay, that example makes a lot more sense.

Changed July 12, 2012 03:17AM UTC by dmethvin comment:6

status: reopenedopen

Changed July 12, 2012 03:18AM UTC by dmethvin comment:7

component: unfiledtraversing

Changed September 11, 2012 02:08AM UTC by mikesherov comment:8

milestone: None1.8.2
owner: → mikesherov
priority: undecidedlow
status: openassigned

Changed September 11, 2012 02:09AM UTC by mikesherov comment:9

Changed September 17, 2012 04:07PM UTC by mikesherov comment:10


Changed October 30, 2012 05:36PM UTC by Mike Sherov comment:11

resolution: → fixed
status: assignedclosed

Fix #12009. $().find( DOMElement ) should pushStack properly. Close gh-927.

Changeset: e8f91514a6f353e85d8117998087903dc7331210