Element nodeName Property - Javascript DOM

Javascript examples for DOM:Element nodeName

Description

The nodeName property returns the name of the specified node.

For an element node, the nodeName property will return the tag name.

For 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.

This property is read-only.

Return Value

A String, representing the name of the node.

Possible values:

  • Returns the tagname for element nodes
  • Returns the name of the attribute for attribute nodes
  • Returns "#text" for text nodes
  • Returns "#comment" for comment nodes
  • Returns "#document" for document nodes

The following code shows how to get the node name of a <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<!-- My personal comment goes here..  -->

<p id="demo"></p>

<script>
function myFunction() {/*from   w  w w . j a  v  a  2 s .com*/
    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;
}
</script>

</body>
</html>

Related Tutorials