jQuery.contains()
Check to see if a DOM element is a descendant of another DOM element.
jQuery.contains(container, contained)🡢 Boolean
container
| Element | The DOM element that may contain the other element. |
contained
| Element | The 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