How to get the type of DOM element using JavaScript ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The task is to get the type of DOM element by having its object reference. Here we are going to use JavaScript to solve the problem. Approach 1: First take the reference of the DOM object to a variable(Here, In this example an array is made of IDs of the element, then select random ID and select that particular element).Use .tagName property to get the element name. Example 1: This example uses the approach discussed above. html <!DOCTYPE html> <html> <body> <h1 id="h1" style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"></p> <button id="button" onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"></p> <script> let up = document.getElementById('GFG_UP'); let down = document.getElementById('GFG_DOWN'); let arr = ["h1", "GFG_UP", "button", "GFG_DOWN"]; up.innerHTML = "Click on the button to check the type of element."; function GFG_Fun() { let id = arr[(Math.floor(Math.random() * arr.length))]; down.innerHTML = "The type of element of id = '" + id + "' is " + document.getElementById(id).tagName; } </script> </body> </html> Output: How to get the type of DOM element using JavaScript? Approach 2: First take the reference of the DOM object to a variable(Here, In this example an array is made of IDs of the element, then select random ID from the array and select that particular element.Use .nodeName property to get the element name. Example 2: This example uses the approach discussed above. html <!DOCTYPE html> <html> <body> <h1 id="h1" style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"></p> <button id="button" onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"></p> <script> let up = document.getElementById('GFG_UP'); let down = document.getElementById('GFG_DOWN'); let arr = ["h1", "GFG_UP", "button", "GFG_DOWN"]; up.innerHTML = "Click on the button to check the type of element."; function GFG_Fun() { let id = arr[(Math.floor(Math.random() * arr.length))]; down.innerHTML = "The type of element of id = '" + id + "' is " + document.getElementById(id).nodeName; } </script> </body> </html> Output: How to get the type of DOM element using JavaScript? Comment More infoAdvertise with us Next Article How to get the class of a element which has fired an event using JavaScript/JQuery? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to get the native type of a value in JavaScript ? JavaScript variables can store any type of value. The JavaScript typeof operator can be used to find out the native type of value. It returns a string value indicating the type of the value. Syntax: typeof(value) Possible output values of typeof: TypeResultundefined"undefined"null"object"boolean"boo 1 min read How to select DOM Elements in JavaScript ? Selecting DOM (Document Object Model) elements is a fundamental aspect of web development with JavaScript. It allows developers to interact with and manipulate elements on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating content, adding event listeners, or mo 3 min read How to Check if element exists in the visible DOM in JavaScript ? This article will show you how to check whether an element exists in the visible DOM or not. For that purpose, there are several methods used but we're going to look at a few of them. Example 1: In this example, the element is searched by document.getElementById('Id') and !! operator is used before 2 min read How To Get Element By Class Name In JavaScript ? When working with the DOM in JavaScript, selecting elements by their class names is a common task. JavaScript provides several methods to achieve this, whether we need to select one or multiple elements. In this article, we will cover different approaches to get elements by class name in JavaScript. 3 min read How to get the class of a element which has fired an event using JavaScript/JQuery? One can use JavaScript and JQuery to retrieve the class of an element that triggers an event. By capturing the event and accessing the class attribute, developers can dynamically identify the specific element involved in the interaction. Below are the methods to get the class of an element that has 3 min read How to get the class of a element which has fired an event using JavaScript/JQuery? One can use JavaScript and JQuery to retrieve the class of an element that triggers an event. By capturing the event and accessing the class attribute, developers can dynamically identify the specific element involved in the interaction. Below are the methods to get the class of an element that has 3 min read Like