jQuery.contains()

Check to see if a DOM element is a descendant of another DOM element.

jQuery.contains(container, contained)🡢 Boolean

container ElementThe DOM element that may contain the other element.
contained ElementThe DOM element that may be contained by (a descendant of) the other element.

The $.contains() method returns true if the DOM element provided by the second argument is a descendant of the DOM element provided by the first argument, whether it is a direct child or nested more deeply. Otherwise, it returns false. Only element nodes are supported; if the second argument is a text or comment node, $.contains() will return false.

Note: The first argument must be a DOM element, not a jQuery object or plain JavaScript object.

Check if an element is a descendant of another.

HTML
$.contains(document.documentElement, document.body); // true
$.contains(document.body, document.documentElement); // false
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)