English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM attributes property

HTML DOM Element Object

attributesAttributes return the active collection of all attribute nodes registered to the specified element node.

Nodes can be accessed by index, starting from 0.

Use the length property of the NamedNodeMap object to determine the number of attributes.

HTML attributes are attribute nodes, allProperties and methodsCan be used for Attribute objects.

Syntax:

element.attributes
var len = document.querySelector("img").attributes.length;
Test and See‹/›

Browser compatibility

All browsers fully support the attribute property:

Properties
attributesIsIsIsIsIs

Technical Details

Return Value:NamedNodeMap object, representing a collection of node attributes
DOM Version:DOM Level1

More Examples

Get the second attribute of the IMG element (index1)name:

var x = document.querySelector("img").attributes[1].name;
Test and See‹/›

Get the second attribute of the IMG element (index1)value:

var x = document.querySelector("img").attributes[1].value;
Test and See‹/›

Traverse all properties of the IMG element and output the name and value of each property:

var attrList = document.querySelector("img").attributes;
var txt = "";
for (let i = 0; i < attrList.length; i++) {
txt += attrList[i].name + " = " + attrList[i].value + "<br>";
}
Test and See‹/›

HTML DOM Element Object