HTML DOM Attributes length property

Definition and usage

length The attribute returns the number of nodes in the NamedNodeMap.

length Attributes are read-only.

Note:The attributes of an HTML element are located in the 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 ist eine Eigenschaft von DOM Level 1 (1998).

Es wird von allen Browsern unterstützt:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Unterstützung 9-11 Unterstützung Unterstützung Unterstützung Unterstützung