HTML DOM Element contains() method

Definition and usage

If the node is a descendant of a node, then contains() The method returns true.

otherwise,contains() The method returns false.

Note:Descendants can be children, grandchildren, great-grandchildren, etc.

Example

Is "mySPAN" a descendant of "myDIV"?

const span = document.getElementById("mySPAN");
let answer = document.getElementById("myDIV").contains(span);

Try it yourself

Syntax

node.contains(node)

Parameter

Parameter Description
node Required. A node that may be a descendant of the node.

Return value

true - The node is a descendant false - The node is not a descendant
Type Description
Boolean

Browser support

element.contains() It is a DOM Level 1 (1998) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support