nodeName
and tagName
are the properties to get the name of HTML node.
tagName
is used to get the type of element nodes that have node type of 1. For other type of nodes such as attribute, comment, text, .etc,
nodeName
is used to get the name of node.
Let's take a look at the following example where we have a simple button:
<button id="login">Login</button>
The nodeName
and tagName
properties are the same for the HTML element:
const button = document.getElementById('login');
button.nodeType;
button.nodeName;
button.tagName;
Let's access the node represents the id
attribute:
const idNode = button.getAttributeNode('id');
idNode.nodeType;
idNode.nodeName;
idNode.tagName;
In a similar way, the button text node gives different result for nodeName
and tagName
:
const content = button.firstChild;
content.nodeType;
content.nodeName;
content.tagName;