HTML DOM Attributes item() Method

Get the name of the first attributes of an element

Definition and Usage

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:

The nodemap.length Property

The nodemap.getNamedItem Method

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 nodeThe 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";

Change the class (the color) of an element:

document.getElementById("myDiv").attributes[1].value = "class2";

Browser Support

attributes.item() is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes 9-11 Yes Yes Yes Yes