Open In App
Related Articles

HTML DOM nodeType Property

Improve Article
Improve
Save Article
Save
Like Article
Like

The DOM nodeType Property is used to find out the type of the node that we are referring to. The type for a particular node is returned in numerical form. DOM nodeType property is a read-only property. 

Return value : It returns a numeric value as according to the type of node.

  • 1: If node is an element node.
  • 2: If node is an attribute node.
  • 3: If node is a text node.
  • 8: If node is a comment node.

Syntax:

 nodeName.nodeType

Example-1: 

html




<!DOCTYPE html>
<html>
  
<head>
    <title>
      HTML|DOM nodeType Property
    </title>
    <style>
        p,
        h5 {
            color: green;
        }
    </style>
</head>
  
<body style="text-align: center;">
    <p id="gfg">
      GeeksforGeeks
    </p>
    <h5>
      Welcome to GeeksforGeeks
    </h5>
  
    <button onclick="node()">
        Click here to know the node type.
    </button>
  
    <p id="nodetype"></p>
  
    <script>
        function node() {
            var geek = document.getElementById("gfg").nodeType;
  
            // innerHTML fetches value of element 
            // valued "nodetype" 
            // and update it with new value of variable
            // "geek"(nodeType value of geek)
            document.getElementById("nodetype").innerHTML = geek;
        }
    </script>
</body>
  
</html>


Output: 

Before:

  

After:

  

Example-2: 

html




<!DOCTYPE html>
<html>
  
<head>
    <title>HTML|DOM nodeType Property</title>
    <style>
        h5 {
            color: green;
        }
    </style>
</head>
  
<body style="text-align: center;">
    <h3>Welcome to GeeksforGeeks</h3>
  
    <button onclick="nType()">
        Click here to know the node type.
    </button>
  
    <p id="nodetype"></p>
  
    <script>
        // nType() function is used to fetch our node
        // and find out its type 
        function nType() {
            var geek = document.body.nodeType;
            document.getElementById("nodetype").innerHTML = geek;
        }
    </script>
</body>
  
</html>


Output: 

Before:

  

After:

  

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Internet Explorer 6 and above
  • Firefox 1 and above
  • Opera 7 and above
  • Safari 1.1 and above

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials