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