HomeLanguagesJavascriptHow to check if the clicked element is a div or not...

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

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:
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32712 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7086 POSTS0 COMMENTS
Nicole Veronica
12226 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12309 POSTS0 COMMENTS
Shaida Kate Naidoo
7202 POSTS0 COMMENTS
Ted Musemwa
7469 POSTS0 COMMENTS
Thapelo Manthata
7187 POSTS0 COMMENTS
Umr Jansen
7171 POSTS0 COMMENTS