Skip to main content

HTML DOM nodeType Property in JAVA SCRIPT

Definition and Usage nodeType..
The nodeType property returns the node type, as a number, of the specified node.
If the node is an element node, the nodeType property will return 1.
If the node is an attribute node, the nodeType property will return 2.
If the node is a text node, the nodeType property will return 3.
If the node is a comment node, the nodeType property will return 8.
This property is read-only.
Return Value: A Number, representing the node type of the node
Syntax : node.nodeType
Differents way how to declare nodeType Property in program.
CODING:
<!DOCTYPE html>
<html>
<body><!-- comment node -->
<h4>nodeName  nodeType</h4>
<p>Click the button get the node name, node type and node value in body tag of all chid node</p>
<button onclick="info()">View</button>
<p id="p"></p>
<script>
function info() {
    var c = document.body.childNodes;//also declare dedocument.body.children;
    var info="";
    for (var i = 0; i < c.length; i++) {
        info +=  "The node name: " + c[i].nodeName + "<br>";
        info += "The node type: " + c[i].nodeType+ "<br>";
    }
    document.getElementById("p").innerHTML = info + c.length + " Tag in body.";
}
</script>
</body>
</html>

Comments