The HTML DOM name property returns a string corresponding to the name of the attribute of an element.
Following is the syntax −
Returning string value
elementAttribute.name
Let us see an example of HTML DOM name property −
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM name</title>
<style>
* {
padding: 2px;
margin:5px;
}
form {
width:70%;
margin: 0 auto;
text-align: center;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-name</legend>
<img src="https://www.tutorialspoint.com/data_communication_computer_network/images/dcn- mini-logo.jpg">
<div id="divDisplay">What attribute in img element is used as a source path?</div>
<input type="button" value="Get Answer" onclick="getAnswers()">
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
function getAnswers() {
var ans = document.getElementsByTagName('img')[0].attributes[0].name;
divDisplay.textContent = 'A \"'+ans+'\" attribute is used as a source path in img element';
}
</script>
</body>
</html>Output
Before clicking ‘Get Answer’ button −

After clicking ‘Get Answer’ button −
