HTML DOM Attributes length Property

Definition and Usage

length Attributes return the number of nodes in NamedNodeMap.

length Attributes are read-only.

Note:HTML element attributes are located in NamedNodeMap.

See Also:

nodemap.item() Method

Instance

Example 1

Get the number of attributes of an HTML element:

let num x = document.getElementById("myButton").attributes.length;

Try It Yourself

Example 2

Get all attribute names:

const nodeMap = document.getElementById("myButton").attributes;
let text = "";
for (let i = 0; i < nodeMap.length; i++) {
  text += nodeMap[i].name + "<br>";
}

Try It Yourself

Example 3

How many attributes does "myImg" have:

let num = document.getElementById("myImg").attributes.length;

Try It Yourself

Example 4

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

Syntax

namednodemap.length

Technical Details

Return Value

Number, representing the number of attribute nodes in the nodemap.

Browser Support

attributes.length Is DOM Level 1 (1998) Feature.

All browsers support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support