Windows Dev Center

item method

Retrieves an attribute for an element from an attributes collection.

Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5



var retval = attributes.item(name);


name [in]

Type: Variant

Variant of type Integer or String that specifies the attribute. If this parameter is an integer, it is the zero-based index of the attribute to be retrieved from the attributes collection. If this parameter is a string, the attribute whose name matches the string is retrieved.

Return value

Type: Object

Returns an attribute if successful, otherwise null.

Standards information


This method returns an error if the attribute is not found. When the item method gets an attribute by name, name must match the case of the attribute.


This example uses the item method to get the name and value of each attribute for an element, and to determine whether the attribute has been specified.

<script type="text/javascript">
function Init()
    oAttrColl = oElem.attributes;
    for (i = 0; i < oAttrColl.length; i++)
        oAttr = oAttrColl.item(i);
        bSpecified = oAttr.specified;
        sName = oAttr.nodeName;
        vValue = oAttr.nodeValue;
        console.log(sName + ": " + bSpecified + ": " + vValue);
<body onload="Init()">
<p id="oElem">An element.</p>

See also




© 2015 Microsoft