HTML DOM Element nodeName
Example
Return the node name of a <p> element:
document.getElementById("myP").nodeName;
Try it Yourself »
Return the node name of the <body> element:
document.body.nodeName;
Try it Yourself »
Get the node names of the <body> element's child nodes:
const nodes = document.body.childNodes;
let text = "";
for (let i = 0; i < nodes.length; i++) {
text += nodes[i].nodeName + "<br>";
}
Try it Yourself »
More examples below.
Description
The nodeName
property returns the name of a node:
The tagname (in upper case) for element nodes |
The attribute name for attribute nodes |
#text for text nodes |
#comment for comment nodes |
#document for document nodes |
The nodeName
property is read-only.
Syntax
element.nodeName
or
node.nodeName
Return Values
The tagname (in upper case) for element nodes |
The attribute name for attribute nodes |
#text for text nodes |
#comment for comment nodes |
#document for document nodes |
More Examples
Get the node name, value and type of "myDIV"s first child node:
const x = document.getElementById("myDIV").firstChild;
let text = "";
text += "Name: " + x.nodeName + "<br>";
text += "Value: " + x.nodeValue + "<br>";
text += "Type: " + x.nodeType;
Try it Yourself »
Browser Support
element.nodeName
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.