Example
Get the node name of a <p> element:
var x = document.getElementById("myP").nodeName;
The result of x will be:
P
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
Property | |||||
---|---|---|---|---|---|
nodeName | Yes | Yes | Yes | Yes | Yes |
Syntax
node.nodeName
Technical Details
Return Value: |
A String, representing the name of the node.
Possible values:
|
---|---|
DOM Version | Core Level 1 Node Object |
More Examples
Example
Get the node name of the <body> element:
var x = document.body.nodeName;
The result of x will be:
BODY
Try it Yourself »
Example
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;
The result of txt will be:
#text
P
#text
BUTTON
#text
P
#text
#comment
#text
DIV
#text
P
#text
SCRIPT
#text
Try it Yourself »
Example
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>
<script>
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;
</script>
The result of txt will be:
The node name: #text
The node value: This is a div element.
The node
type: 3
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