HTML | DOM nodeType Property

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Before:

After:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Before:

After:

Supported Browsers:

  • Google Chrome
  • Internet Explore
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.