Open In App
Related Articles

HTML DOM contains() Method

Improve Article
Save Article
Like Article

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 the node is a descendant of the given node else false. 

Example: In this example, we will use contains() method


<!DOCTYPE html>
    <title>HTML DOM contains() Method</title>
    <!--script to check, if a node contains descendant-->
        function containchild() {
            let span =
            let div =
            document.getElementById("result").innerHTML = div;
        <h1 style="color:green">
            Welcome to GeeksforGeeks</h1>
    <div id="para1">
        <p>Social media is not about the
            <span id="sp1">exploitation of technology</span>
            but service to community.
    <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>



Supported Browsers: The browser supported by DOM contain() Method listed below:

  • Google Chrome 16 and above
  • Edge 12 and above
  • Internet Explorer 9 and above
  • Firefox 9 and above
  • Opera 7 and above
  • Safari 1.1 and above

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 15 Jun, 2023
Like Article
Save Article
Similar Reads