Skip to content
Related Articles

Related Articles

Improve Article

HTML | DOM nodeType Property

  • Last Updated : 26 Jul, 2019

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:




<!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:




<!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
  • Internet Explore
  • Firefox
  • Opera
  • Safari

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :