How to get the type of DOM element using JavaScript ? Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share 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 select DOM Elements in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read 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 the outer html of an element using jQuery ? Sometimes, there is a need to get the entire HTML element by its id and not merely its contents, for doing so, we shall use the HTML DOM outerHTML Property to get the outer HTML of HTML element. Syntax: document.getElementById("your-element-id").outerHTML) You can use a variable and initialize it to 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 Like