Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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:

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 : 30 Jan, 2020
Like Article
Save Article
Similar Reads
Related Tutorials