Node: contains() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thecontains() method of theNode interfacereturns a boolean value indicatingwhether a node is a descendant of a given node, that is the node itself,one of its direct children (childNodes),one of the children's direct children, and so on.
Note:A node iscontained inside itself.
In this article
Syntax
contains(otherNode)Parameters
Return value
A boolean value that istrue ifotherNode is contained in the node,false if not.
If theotherNode parameter isnull,contains() always returnsfalse.
Example
This function checks to see if an element is in the page's body. Ascontains is inclusive and determining if the body contains itself isn't theintention ofisInPage this case explicitly returnsfalse.
function isInPage(node) { return node === document.body ? false : document.body.contains(node);}Specifications
| Specification |
|---|
| DOM> # ref-for-dom-node-contains①> |