The HTML DOM nodeName property returns a string corresponding to the name of the node.
Following is the syntax −
Returning string value
Node.nodeName
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 −
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM nodeName</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-nodeName</legend>
<label for="example">Example site: </label>
<input id="urlSelect" type="url" name="urlAddress" value="https://www.example.com" disabled>
<input type="button" onclick="getNodeNames()" value="Get all names">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var urlSelect = document.getElementById("urlSelect");
var commentNode = document.createComment("My Example URL");
urlSelect.appendChild(commentNode);
var divDisplay = document.getElementById("divDisplay");
function getNodeNames() {
divDisplay.textContent = 'Body: '+document.body.nodeName;
divDisplay.textContent += ' & input: '+urlSelect.nodeName;
divDisplay.textContent += ' & comment: '+commentNode.nodeName;
}
</script>
</body>
</html>Output
Before clicking ‘Get all names’ button −

After clicking ‘Get all names’ button −
