course recommendation:

html dom element attributes property

attributes return the collection of attributes in the element. definition and usage

attributes return the collection of attributes in the element. attributes

NamedNodeMap

NamedNodeMap returns NamedNodeMap.

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

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

.

nodes can be accessed by name or index (subscript). The index starts from 0.

see also:

html dom attributes

instance

example 1

how many attributes does the <img> element have:

try it yourself

example 2

show 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 the <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 collection of attributes.

browser support

element.attributes dom level 1 (1998) iala.

Dukkanin browsersu suna aiki da ita a hankali:

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