HTML DOM Attributes item() method

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:

nodemap.length property

nodemap.getNamedItem method

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;

Try it yourself

const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap[0].name;
let name2 = nodeMap[1].name;

Try it yourself

Example 2

Change the element's class (color):

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

Try it yourself

Example 3

Change the element's class (color):

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

Try it yourself

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