revision:
It returns a collection of attributes in an element, i.e. a NamedNodeMap : an array-like unordered collection of an element's attributes or a list of Attr objects.
Its "length" property returns the number of nodes, which can be accessed by name or index numbers. The index starts at 0.
Syntax:
node.attributes : returns a collection of attribute objects
property value:
none :
example
<div>
<img id="myImg" alt="holiday" src="../../pics/1.jpg" width="150" height="113">
<p id="prop3"></p>
<p id="prop4"></p>
</div>
<script>
let num = document.getElementById("myImg").attributes.length;
document.getElementById("prop3").innerHTML = "NameNodeMap : " + num;
const nodeMap = document.getElementById("myImg").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
text += nodeMap[i].name + " = " + nodeMap[i].value + "<br>";
}
document.getElementById("prop4").innerHTML = text;
</script>
Click "try it" to display the numer of button attributes:
<div>
<p>Click "try it" to display the numer of button attributes:</p>
<button id="myButton" onclick="firstFunction()">Try it</button>
<p id="demo"></p>
</div>
<script>
function firstFunction() {
let numb = document.getElementById("myButton").attributes.length;
document.getElementById("demo").innerHTML = numb;
}
</script