Node.contains()

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
DOM
The definition of 'Node.contains()' in that specification.
Living Standard Initial definition

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
containsChrome Full support 16Edge Full support 12Firefox Full support 9IE Full support 9
Notes
Full support 9
Notes
Notes Only supports contains for HTML elements and not for SVG elements.
Opera Full support 7Safari Full support 1.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 9Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support
Full support
See implementation notes.
See implementation notes.

See also