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:
Recommended Posts:
- How to show a span element for each rows clicked in AngularJS?
- How to select all text in HTML text input when clicked using JavaScript?
- How to get the ID of the clicked button using JavaScript / jQuery ?
- How to detect when cancel is clicked on file input using JavaScript ?
- How to overlay one div over another div using CSS
- How to copy the content of a div into another div using jQuery ?
- How to center a div within another div?
- How to clear all div's content inside a parent div ?
- How to make the images bigger when clicked?
- Print the content of a div element using JavaScript
- How to append data to <div> element using JavaScript ?
- How to rotate an HTML div element 90 degrees using JavaScript ?
- How to hide div element by default and show it on click using JavaScript and Bootstrap ?
- How to remove specific div element by using JavaScript?
- How to scroll to an element inside a div using javascript?
- How to make div not larger than its contents using CSS?
- jQuery | Create a div element
- How to create a div element in jQuery ?
- Display div element on hovering over <a> tag using CSS
- How to hide div element after few seconds in jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


