MDN Web Docs

Syntax

js

contains(otherNode)

Parameters

otherNode

The Node to test with.

Note: otherNode is not optional, but can be set to null.

Return value

A boolean value that is true if otherNode is contained in the node, false if not.

If the otherNode parameter is null, contains() always returns false.

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.

js

function isInPage(node) {
  return node === document.body ? false : document.body.contains(node);
}

Specifications

Specification
DOM
# ref-for-dom-node-containsâ‘ 

Browser compatibility

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗