HTML DOM Element attributes attribute
- Previous Page appendChild()
- Next Page blur()
- Go Up One Level HTML DOM Elements Object
Definition and usage
attributes
attribute returns a collection of attributes in the element.
attributes
attribute returns NamedNodeMap.
NamedNodeMap
NamedNodeMap an unordered collection of element attributes similar to an array.
In other words: NamedNodeMap is Attr object.
NamedNodeMap provides a list that can return the number of nodes. length property.
Nodes can be accessed by name or index (index). The index starts from 0.
See also:
Instance
Example 1
How many attributes does the <img> element have:
let numb = document.getElementById("myImg").attributes.length;
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;
Example 3
How many attributes does the <button> element have:
let numb = document.getElementById("myButton").attributes.length;
Example 4
Get the name of the second (index 1) attribute of a <button> element:
let attr = document.getElementById("myBtn").attributes[1].name;
Syntax
node.attributes
Return value
Type | Description |
---|---|
NamedNodeMap | a collection of attribute objects. |
Browser support
element.attributes
It 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 |
- Previous Page appendChild()
- Next Page blur()
- Go Up One Level HTML DOM Elements Object