Open In App
Related Articles

How to check if the clicked element is a div or not in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Given an HTML document, which contains many elements. The task is to determine whether the clicked element is DIV or not with the help of JavaScript. Two approaches are discussed here, first approach uses the tagName property of element and second uses instanceOf operator to search for DIV. Approach 1:
  • Use element.tagName property, which returns the tagName of the element.(eg.. if(el.tagName == ‘DIV’) then it is DIV else not)
Example 1: This example implements the above approach.
<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Check if an element is a div in JavaScript.
    </title>
    <style>
        #div {
            background: green;
            height: 100px;
            width: 200px;
            margin: 0 auto;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;" onClick="GFG_Fun(this.tagName)">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" onClick="GFG_Fun(this.tagName)">
    </p>
    <div id="div" onClick="GFG_Fun(this.tagName)">
        This is Div element.
    </div>
    <p id="GFG_DOWN" style="color: green;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        up.innerHTML = 
          "Click on any element to check if it's DIV or not.";
  
        function GFG_Fun(tagName) {
            // checking if the tagName is equal to 'DIV'.
            if (tagName == 'DIV') {
                down.innerHTML = "It's a DIV Element";
            } else {
                down.innerHTML = "It's not a DIV Element";
            }
        }
    </script>
</body>
  
</html>

                    
Output:
  • Before clicking on element:
  • After clicking on element:
Approach 2:
  • Use instanceOf operator, and check whether the element is HTMLDivElement or not.
Example 2: This example implements the above approach.
<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Check if an element is a div in JavaScript.
    </title>
    <style>
        #div {
            background: green;
            height: 100px;
            width: 200px;
            margin: 0 auto;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;" onClick="GFG_Fun(this)">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" onClick="GFG_Fun(this)">
    </p>
    <div id="div" onClick="GFG_Fun(this)">
        This is Div element.
    </div>
    <p id="GFG_DOWN" style="color: green;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        up.innerHTML =
            "Click on any element to check if it's DIV or not.";
  
        function GFG_Fun(el) {
            // checking if the el is instance of HTMLDivElement
            if (el instanceof HTMLDivElement) {
                down.innerHTML = "It's a DIV Element";
            } else {
                down.innerHTML = "It's not a DIV Element";
            }
        }
    </script>
</body>
  
</html>

                    
Output:
  • Before clicking on element:
  • After clicking on element:


Last Updated : 30 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads