HTML DOM Attributes length
Example
Get the number of attributes of an HTML element:
let num x = document.getElementById("myButton").attributes.length;
Try it Yourself »
More examples below.
Description
The length
property returns the number of nodes in a NamedNodeMap.
The length
property is read-only.
Note
The attributes of an HTML element is located in a NamedNodeMap.
See Also:
Syntax
namednodemap.length
Technical Details
Return Value: | A Number, representing the number of attribute nodes in the nodemap |
---|
More Examples
Get the name of all attributes:
const nodeMap = document.getElementById("myButton").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
text += nodeMap[i].name + "<br>";
}
Try it Yourself »
How many attributes does "myImg" have:
let num = document.getElementById("myImg").attributes.length;
Try it Yourself »
Get all attributes:
const nodeMap = document.getElementById("myImg").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
text += nodeMap[i].name + " = " + nodeMap[i].value + "<br>";
}
Try it Yourself »
Browser Support
attributes.length
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |