HTML DOM nodeName Property

The HTML DOM nodeName property returns a string corresponding to the name of the node.

Following is the syntax −

Returning string value


Here, the return value can be the following −

  • Tag name in uppercase for element nodes
  • Name of the attribute for attribute nodes
  • ‘#comment’ for comment nodes
  • ‘#document’ for comment nodes
  • ‘#text’ for text nodes

NOTE: Whitespaces are considered as text nodes only.

Let us see an example of HTML DOM nodeName property −


 Live Demo

<!DOCTYPE html>
<title>HTML DOM nodeName</title>
   form {
      margin: 0 auto;
      text-align: center;
   * {
      padding: 2px;
   input[type="button"] {
      border-radius: 10px;
         <label for="example">Example site: </label>
         <input id="urlSelect" type="url" name="urlAddress" value="" disabled>
         <input type="button" onclick="getNodeNames()" value="Get all names">
         <div id="divDisplay"></div>
   var urlSelect = document.getElementById("urlSelect");
   var commentNode = document.createComment("My Example URL");
   var divDisplay = document.getElementById("divDisplay");
   function getNodeNames() {
      divDisplay.textContent = 'Body: '+document.body.nodeName;
      divDisplay.textContent += ' & input: '+urlSelect.nodeName;
      divDisplay.textContent += ' & comment: '+commentNode.nodeName;


Before clicking ‘Get all names’ button −

After clicking ‘Get all names’ button −