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.


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.






<!DOCTYPE html>
    <title>HTML | DOM contains() Method</title>
    <!--script to check, if a node contains descendant-->
        function containchild() {
            var span = 
            var div = 
            document.getElementById("result").innerHTML = div;
        <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>
    <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>


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
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.