Skip to main content

Bug Tracker

Side navigation

#11353 closed bug (invalid)

Opened February 15, 2012 04:49PM UTC

Closed February 15, 2012 07:26PM UTC

Last modified February 16, 2012 01:48PM UTC

$.contains returns true if elements are different and lack 'contains' methods

Reported by: rjberry@gmail.com Owned by:
Priority: low Milestone: None
Component: misc Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description

e.g.

elem1 = $('<div></div>');

elem2 = $('<p></p>');

$.contains(elem1, elem2); = true

$.contains(elem2, elem1); = true

Attachments (0)
Change History (4)

Changed February 15, 2012 07:26PM UTC by sindresorhus comment:1

component: unfiledmisc
priority: undecidedlow
resolution: → invalid
status: newclosed

$.contains docs:

Check to see if a DOM element is within another DOM element.

$.contains doesn't support jQuery objects, only vanilla DOM elements.

Changed February 15, 2012 10:40PM UTC by dmethvin comment:2

In general a non-chained method like $.method usually accepts a DOM element, not a jQuery object. It's intended as a low-level method.

Changed February 16, 2012 09:54AM UTC by rjberry@gmail.com comment:3

Should it not return false then or even better throw an error? Why do JavaScript libraries so rarely throw errors? Is this against some kind of policy?

Changed February 16, 2012 01:48PM UTC by dmethvin comment:4

The reason that JavaScript APIs don't usually throw errors intentionally is that it is expensive to check all the argument counts and types. Instead, we document the valid inputs and outputs of the API at http://api.jquery.com . I don't know what $.contains([{}], {}) or $.contains(new Date, "March", 4) does but those are not valid calls.