HTML DOM nodeName Property

Element Object


Get the node name of a <p> element:

var x = document.getElementById("myP").nodeName;
Try it Yourself »

More "Try it Yourself" examples below.

Definition and Usage

The nodeName property returns the name of the specified 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.

For other node types, the nodeName property will return different names for different node types.

Tip: You can also use the tagName property to return the tag name of an element. The difference is that tagName only return tag names, while nodeName returns the name of all nodes (tags, attributes, text, comments).

This property is read-only.

Browser Support

nodeName Yes Yes Yes Yes Yes



Technical Details

Return Value: A String, representing the name of the node.

Possible values:

  • Returns the tagname for element nodes, in uppercase
  • Returns the name of the attribute for attribute nodes
  • Returns "#text" for text nodes
  • Returns "#comment" for comment nodes
  • Returns "#document" for document nodes
DOM Version Core Level 1 Node Object

More Examples


Get the node name of the <body> element:

var x = document.body.nodeName;
Try it Yourself »


Get the node names of the <body> element's child nodes:

var c = document.body.childNodes;
var txt = "";
var i;
for (i = 0; i < c.length; i++) {
    txt = txt + c[i].nodeName + "<br>";

document.getElementById("demo").innerHTML = txt;
Try it Yourself »


Get the node name, node value and the node type of the <div> element's first child node:

<div id="myDIV">This is a div element.</div>

var x = document.getElementById("myDIV").firstChild;
var txt = "";
txt += "The node name: " + x.nodeName + "<br>";
txt += "The node value: " + x.nodeValue + "<br>";
txt += "The node type: " + x.nodeType;
Try it Yourself »

Related Pages

HTML DOM reference: element.tagName Property

HTML DOM reference: node.nodeType Property

HTML DOM reference: node.nodeValue Property

HTML DOM reference: node.childNodes Property

Element Object