HTML DOM Attributes item() method
- Previous page isId
- Next page length
- Go back to the previous level HTML DOM Attributes
Definition and usage
item()
The method returns a Node object with the namedNodeMap at the specified index.
Note:Nodes are sorted in the order they appear in the source code. The index starts at 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 |
The attribute node at the specified index. Returns null if the index is out of range. |
Browser support
attributes.item()
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 back to the previous level HTML DOM Attributes