Web/API/Node/contains

From Get docs
< Web/API‎ | Node


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

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
contains Chrome

Full support 16

Edge

Full support 12

Firefox

Full support 9

IE Partial support 9

Notes'

Partial support 9

Notes'

Notes' Only supported for HTMLElement, not all Node objects.

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