course recommendation:

HTML DOM Element attributes attribute

returns a collection of attributes in the element. definition and usage

returns a collection of attributes in the element. attributes

NamedNodeMap

NamedNodeMap returns NamedNodeMap.

an unordered collection of element attributes similar to an array. In other words: NamedNodeMap isAttr object

. NamedNodeMap provides a list that can return the number of nodes.length property

.

Nodes can be accessed by name or index (subscript). The index starts at 0.

see also:

HTML DOM Attribute

instance

Example 1

How many attributes does an <img> element have:

try it yourself

Example 2

Display all attributes of the <img> element:

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("demo").innerHTML = text;

try it yourself

Example 3

How many attributes does a <button> element have:

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

try it yourself

Example 4

Get the name of the second (index 1) attribute of a <button> element:

let attr = document.getElementById("myBtn").attributes[1].name;

try it yourself

syntax

node.attributes

return value

type description
NamedNodeMap a collection of attribute objects.

browser support

element.attributes is a feature of DOM Level 1 (1998).

All browsers fully support it:

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