THE WORLD'S LARGEST WEB DEVELOPER SITE

HTML DOM contains() Method

Element Object

Example

Find out if a <span> element is a descendant of a <div> element:

var span = document.getElementById("mySPAN");
var div = document.getElementById("myDIV").contains(span);
Try it Yourself »

Definition and Usage

The contains() method returns a Boolean value indicating whether a node is a descendant of a specified node.

A descendant can be a child, grandchild, great-grandchild, and so on.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
contains() Yes 5.0 9.0 3.0 Yes

Syntax

node.contains(node)

Parameter Values

Parameter Description
node Required. Specifies the node that may be contained by (a descendant of) a specified node

Technical Details

Return Value: A Boolean, indicating whether a node is a descendant of a specified node:
  • true - The node is a descendant
  • false - The node is NOT a descendant

Element Object