Javascript DOM HTML Node nodeName Property

Introduction

Get the node name of a <p> element:

var x = document.getElementById("myP").nodeName;

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="myP">Click the button to get the node name of this element.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from w  w  w  . j av  a  2  s.com*/
  var x = document.getElementById("myP").nodeName;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The nodeName property returns a String representing the name of the node.

If the node is an element node, the nodeName property will return the tag name.

If the node is an attribute node, the nodeName property will return the name of the attribute.

You can use the tagName property to return the tag name of an element.

The tagName only return tag names.

The nodeName returns the name of all nodes: tags, attributes, text, comments.

This property is read-only.

Possible values:

  • the tagname for element nodes, in uppercase
  • the name of the attribute for attribute nodes
  • "#text" for text nodes
  • "#comment" for comment nodes
  • "#document" for document nodes



PreviousNext

Related