FREE Web Template Download
HTML CSS JAVASCRIPT SQL PHP BOOTSTRAP JQUERY ANGULARJS TUTORIALS REFERENCES EXAMPLES Blog
 

HTML DOM item() Method

Attribute Object Reference Attribute Object

Example

Get the name of the first attribute of a <button> element:

var x = document.getElementsByTagName("BUTTON")[0].attributes.item(0).nodeName;

The result of x could be:

onclick
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The item() method returns the node at the specified index in a NamedNodeMap, as a Node object.

The nodes are sorted as they appear in the source code, and the index starts at 0.

Note: There are two ways to access an attribute node at the specified index in a NamedNodeMap:

This syntax:

document.getElementsByTagName("BUTTON")[0].attributes.item(1);   // The 2nd attributeTry it

Will produce the same result as this syntax:

document.getElementsByTagName("BUTTON")[0].attributes[1];        // The 2nd attributeTry it

You can use whatever method you like, however, the most common method is [index].

Tip: Use the length property to return the number of nodes in a NamedNodeMap object.


Browser Support

Method
item() Yes Yes Yes Yes Yes

Syntax

namednodemap.item(index)

or simply:

namednodemap[index]

Parameter Values

Parameter Type Description
index Number Required. The index of the node in the NamedNodeMap you want to return

Technical Details

Return Value: A Node object, representing the attribute node at the specified index.

Note: Returns null if the index number is out of range
DOM Version Core Level 1

Examples

More Examples

Example

Change the value of a <button> element's second attribute:

document.getElementsByTagName("BUTTON")[0].attributes[1].value = "newClass";
Try it Yourself »

Attribute Object Reference Attribute Object