HTML | DOM contains() Method
The contains() method is used to find whether the specified node is a descendant of the given node.
This descendant can be a child, grandchild, great-grandchild, and so on.
Syntax:
node.contains( otherNode )
Parameters: The “othernode” in the syntax is the parameter required in this function.
Return Value: It returns true if node is descendant of the given node else false.
Example:
<!DOCTYPE html> <html> <head> <!--script to check, if a node contains descendant--> <script> function containchild() { var span = document.getElementById("sp1"); var div = document.getElementById("para1").contains(span); document.getElementById("result").innerHTML = div; } </script> </head> <body> <center> <h1 style="color:green"> Welcome to GeeksforGeeks</h1></center> <div id="para1"> <p>Social media is not about the <span id="sp1">exploitation of technology</span> but service to community.</p> </div> <p>Click the button to find out if the div element contains a span element.</p> <button onclick="containchild()">Check</button> <!--for displaying boolean value --> <p id="result"></p> </body> </html> |
Output:
Before clicking on the check button:

After clicking on the check button:

Supported Browsers: The browser supported by DOM contain() Method are listed below:
- Google Chrome
- Apple Safari
- Firefox
- Opera
- Edge
Recommended Posts:
- HTML | DOM click() Method
- HTML | DOM getNamedItem() Method
- HTML | DOM getElementsByName() Method
- HTML | DOM Storage key() Method
- HTML | DOM getAttributeNode() Method
- HTML | DOM insertAdjacentHTML() Method
- HTML | DOM insertAdjacentElement() Method
- HTML | DOM isSameNode() Method
- HTML | DOM cloneNode() Method
- HTML | DOM getBoundingClientRect() Method
- HTML | DOM removeNamedItem() Method
- HTML | DOM querySelector() Method
- HTML | DOM getElementsByClassName() Method
- HTML | DOM querySelectorAll() Method
- HTML | DOM execCommand() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



