HTML DOM Element contains()

Example

Is "mySPAN" a descendant of "myDIV"?

const span = document.getElementById("mySPAN");
let answer = document.getElementById("myDIV").contains(span);
Try it Yourself »

Description

The contains() method returns true if a node is a descendant of a node.

The contains() method returns false if not.

Note

A descendant can be a child, grandchild, great-grandchild, ...


Syntax

node.contains(node)

Parameters

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

Return Value

Type Description
Booleantrue - The node is a descendant
false - The node is NOT a descendant

Browser Support

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

It is fully supported in all browsers:

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

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.