The Node.contains()
method returns a Boolean
value indicating whether a node is a descendant of a given node, i.e. the node itself, one of its direct children (childNodes
), one of the children's direct children, and so on.
Syntax
node.contains( otherNode )
Example
This function checks to see if an element is in the page's body. As contains
is inclusive and determining if the body contains itself isn't the intention of isInPage
this case explicitly returns false
.
function isInPage(node) { return (node === document.body) ? false : document.body.contains(node); }
Specifications
Specification | Status | Comment |
---|---|---|
DOMThe definition of 'Node.contains()' in that specification. | Living Standard | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
contains
|
Chrome
Full support 16 |
Edge
Full support 12 |
Firefox
Full support 9 |
IE Partial support 9 Partial support 9 Notes' Only supported for |
Opera
Full support 7 |
Safari
Full support 1.1 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android
Full support 9 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
- Partial support
- Partial support
- See implementation notes.'
- See implementation notes.
See also
Node.contains() by Mozilla Contributors is licensed under CC-BY-SA 2.5.