HTML DOM Attributes item() method
- Previous Page isId
- Next Page length
- Go to the Previous Level HTML DOM Attributes
Definition and usage
item()
The method returns a Node object representing the node at the specified index in the namedNodeMap.
Note:Nodes are sorted in the order they appear in the source code. The index starts from 0.
See also:
Instance
Example 1
Get the name of the first attribute of the element:
const nodeMap = document.getElementById("myDiv").attributes; let name1 = nodeMap.item(0).name; let name2 = nodeMap.item(1).name;
const nodeMap = document.getElementById("myDiv").attributes; let name1 = nodeMap[0].name; let name2 = nodeMap[1].name;
Example 2
Change the element's class (color):
document.getElementById("myDiv").attributes.item(1).value = "class2";
Example 3
Change the element's class (color):
document.getElementById("myDiv").attributes[1].value = "class2";
Syntax
namednodemap.item(index)
or abbreviated as:
namednodemap[index]
Parameter
Parameter | Description |
---|---|
index | Required. The index of the attribute node in the NamedNodeMap. |
Return value
Type | Description |
---|---|
Node |
Attribute node at the specified index. Returns null if the index is out of range. |
Browser support
attributes.item()
It is a feature of DOM Level 1 (1998).
All browsers support it:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
- Previous Page isId
- Next Page length
- Go to the Previous Level HTML DOM Attributes