Bug Tracker

Ticket #11353 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

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

Reported by: rjberry@… Owned by:
Priority: low Milestone: None
Component: misc Version: 1.7.1
Keywords: Cc:
Blocking: Blocked by:

Description

e.g.

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

$.contains(elem1, elem2); = true $.contains(elem2, elem1); = true

Change History

comment:1 Changed 3 years ago by sindresorhus

  • Priority changed from undecided to low
  • Resolution set to invalid
  • Status changed from new to closed
  • Component changed from unfiled to misc

$.contains  docs:

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

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

comment:2 Changed 3 years ago by dmethvin

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.

comment:3 Changed 3 years ago by rjberry@…

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?

comment:4 Changed 3 years ago by dmethvin

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.

Note: See TracTickets for help on using tickets.