Ticket #7297 (closed bug: invalid)
Firefox jQuery contains method error
| Reported by: | oleg.richards@… | Owned by: | oleg.richards@… |
|---|---|---|---|
| Priority: | high | Milestone: | 1.5 |
| Component: | core | Version: | 1.5 |
| Keywords: | firefox method error | Cc: | |
| Blocking: | Blocked by: |
Description
The following code:
var x = $('#foo');
$(document).bind('click.popup', function(e) {
if (!$.contains(x, e.target)) {
e.stopPropagation();
return false;
}
x.trigger('popup.hide');
});
Gives me an error in Firefox. In all other browsers it works properly.
Error: g.compareDocumentPosition is not a function Source: http://localhost:8080/public//js/libs/jquery-1.4.3.min.js String: 102
Change History
comment:1 Changed 3 years ago by addyosmani
- Keywords firefox method error added
- Owner set to oleg.richards@…
- Status changed from new to pending
- Priority changed from undecided to low
comment:2 Changed 3 years ago by aaronblondeau@…
Breaks on Firefox 3.6.12 (Mac OSX Snow Leopard): http://jsfiddle.net/gRyZp/
comment:3 Changed 3 years ago by anonymous
Sorry, missing a # in the first selector (doesn't particularly affect outcome though). Here is a better example : http://jsfiddle.net/gRyZp/1/
comment:4 Changed 3 years ago by rwaldron
- Priority changed from low to high
- Status changed from pending to open
- Component changed from unfiled to core
- Milestone changed from 1.5 to 1.4.5
Confirmed; Doesn't return in FF3.6.12
comment:5 Changed 2 years ago by jitter
- Status changed from open to closed
- Resolution set to invalid
This isn't a bug in jQuery but rather a misunderstanding on how the contains method works.
The documentation for jQuery.contains() says:
jQuery.contains( container, contained )
container The DOM element that may contain the other element.
contained The DOM node that may be contained by the other element.
This means you can only pass in real DOM elements to this method. In all test cases linked to on this report there are jQuery objects passed in instead which is clearly invalid. The reason it returns true in some other browsers is actually the real bug in the contains method.
comment:6 Changed 2 years ago by john
- Version changed from 1.4.3 to 1.5
There wasn't a 1.4.5 release, was actually 1.5.
comment:7 Changed 2 years ago by john
- Milestone changed from 1.4.5 to 1.5
There was no 1.4.5 release, was actually 1.5.
comment:8 Changed 2 years ago by anonymous
This is a real error. I get the error message also in firefox.
Error message is "a.compareDocumentPosition is not a function"
I have found a solution though... basically the error only occurs in firefox if you test to see if a jQuery object is in an element. e.g.
jQuery.contains(document, $(document.body)) causes error in firefox
but
jQuery.contains(document, document.body) would work fine.
However the firts on works fine in other browsers.
comment:10 Changed 2 years ago by anonymous
The documentation states that it only works with native DOM elements and not jQuery objects. But I agree with everyone having problems that it should accept jQuery objects as parameters.
A work-around using jQuery is:
$(contained, container).size() == 0
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Could you please submit a complete test-case on jsFiddle.net that reproduces this issue so that we can accurately evaluate the bug experienced?