HTML DOM Attributes item()
Example
Get the name of the first attributes of an element:
const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap.item(0).name;
let name2 = nodeMap.item(1).name;
Try it Yourself »
const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap[0].name;
let name2 = nodeMap[1].name;
Try it Yourself »
More examples below.
Description
The item()
method returns an attribute (by index) from a NamedNodeMap.
Note
The nodes are sorted as they appear in the source code. The index starts at 0.
See Also:
Syntax
namednodemap.item(index)
or simply:
namednodemap[index]
Parameters
Parameter | Description |
index | Required. The index of the attribute node in the NamedNodeMap. |
Return Value
Type | Description |
A node | The attribute node at the specified index. Or null if the index number is out of range. |
More Examples
Change the class (the color) of an element:
document.getElementById("myDiv").attributes.item(1).value = "class2";
Try it Yourself »
Change the class (the color) of an element:
document.getElementById("myDiv").attributes[1].value = "class2";
Try it Yourself »
Browser Support
attributes.item()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.