Node name of an HTML element using javascript?



Node name

Javascript has provided document.nodeName to get the node of an element. The name will be fetched by using the id of the particular node.

syntax

document.nodeName();

Example-1

In the following example, a node is given a tag and using that tag, the method document.nodeName() grabs the node name. Here tag doesn't matter, any kind of tag can result as an output.

<html>
<body>
<div id="dtag">The node name is:</div>
<p id="nname"></p>
<script>
   var x = document.getElementById("dtag").nodeName;
   document.getElementById("nname").innerHTML = x;
</script>
</body>
</html>

Output

The node name is:
DIV

Example-2

In the following example, a node is given a tag and using that tag, the method document.nodeName() grabs the node name. Here p tag results as the output.

<html>
<body>
<p id="ptag">The node namr is:</p>
<p id="nname"></p>
<script>
   var x = document.getElementById("ptag").nodeName;
   document.getElementById("nname").innerHTML = x;
</script>
</body>
</html>

Output

The node name is:
p
raja
Published on 16-Aug-2019 13:11:04
Advertisements