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

 

Syntax

var retval = attributes.item(name);

Parameters

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

Remarks

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.

Examples

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.


<html>
<head>
<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);
    }
}
</script>
</head>
<body onload="Init()">
<p id="oElem">An element.</p>
</body>
</html>


See also

attributes

 

 

Show:
© 2015 Microsoft