HTML DOM Element nodeName attribute

Definition and usage

nodeName The attribute returns the name of the node:

  • If the node is an element node, the nodeName property returnsTag Name(Uppercase).
  • If the node is an attribute node, the nodeName property returnsThe name of the attribute.
  • If the node is a text node, the nodeName property returns #text.
  • If the node is a comment node, the nodeName property returns #comment.
  • If the node is a document node, the nodeName property returns #document.

The nodeName property is read-only.

See also:

nodeType property

nodeValue property

tagName property

childNodes property

Instance

Example 1

Returns the node name of the <p> element:

document.getElementById("myP").nodeName;

Try It Yourself

Example 2

Returns the node name of the <body> element:

document.body.nodeName;

Try It Yourself

Example 3

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

const nodes = document.body.childNodes;
let text = "";
for (let i = 0; i < nodes.length; i++) {
  text += nodes[i].nodeName + "<br>";
}

Try It Yourself

Example 4

Get the node name, value, and type of the first child node of "myDIV":

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

Syntax

element.nodeName

or

node.nodeName

Return Value

  • Returns the element nodeTag Name(Uppercase)
  • Returns the attribute nodeAttribute Name
  • Returns the text node #text
  • Returns the comment node #comment
  • Returns the document node #document

Browser Support

element.nodeName It is a DOM Level 1 (1998) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support