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

30 Jan, 2020
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:
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?