course recommendation:
- Previous page appendChild()
- Next page blur()
- Go back to the previous level HTML DOM Elements object
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.
HTML DOM Attribute
instance
Example 1
How many attributes does an <img> element have:
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;
Example 3
How many attributes does a <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
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 back to the previous level HTML DOM Elements object