English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM nodeName property

HTML DOM Element Object

The nodeName read-only property returns the name of the current node as a string.

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

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.

Note:You can also usetagNameAttribute returns the tag name of the element. The difference is that tagName only returns the tag name, while nodeName returns the names of all nodes (tag, attribute, text, comment).

Syntax:

node.nodeName
var x = document.getElementById("myPara").nodeName;
Test and See‹/›

Browser compatibility

nodeName property is fully supported in all browsers:

Attribute
nodeNameIsIsIsIsIs

Technical details

Return value:String representing the node name.
Possible values:
  • Return the tag name (in uppercase) of the element node

  • Return the attribute name of the attribute node

  • Return the " #text "

  • Return " for comment nodes: #comment "

  • Return the " #document "

DOM version:DOM level1

More examples

Return the node names of the child nodes of the BODY element:

var x = document.body.childNodes;
var txt = "";
for (let i = 0; i < x.length;++) {
txt += x[i].nodeName + "<br>";
}
document.getElementById("para").innerHTML = txt;
Test and See‹/›

Return the node name, node type, and node value of the first child node of div:

<div id="div-1">This is a div element.</div>
<script>
var x = document.getElementById("div-1).firstChild;
var txt = "";
txt +="Node Name: " + x.nodeName + "<br>";
txt +="Node Value: " + x.nodeValue + "<br>";
txt +="Node Type: " + x.nodeType;
document.getElementById("para").innerHTML = txt;
</script>
Test and See‹/›

Related References

HTML DOM Reference:node .childNodes property

HTML DOM Reference:node .firstChild property

HTML DOM Reference:node .lastChild property

HTML DOM Reference:node .parentNode property

HTML DOM Reference:node .previousSibling property

HTML DOM Reference:node .nextSibling property

HTML DOM Element Object