item method
Retrieves an attribute for an element from an attributes collection.
![]() |
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
- Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5
Remarks
Call QueryInterface on pdisp to obtain an IHTMLDOMAttribute interface pointer.
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.
Windows Internet Explorer 8 and later. In IE8 Standards mode, this method no longer indexes the attributes collection by the name property of the attribute object; it now accepts only numeric indexes or quoted numeric indexes. If given a string that is not a numeric index, this method will return the object at index 0. Use getNamedItem to reference an attribute by name. For more information, see Defining Document Compatibility.
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>
This example uses the item method to get the name and value of each attribute for an element, and whether the attribute has been specified.
IHTMLDOMNode* pElemDN; IDispatch* pACDisp; IHTMLAttributeCollection* pAttrColl; IDispatch* pItemDisp; IHTMLDOMAttribute* pItem; LONG lACLength; VARIANT vACIndex; BSTR bstrName; VARIANT vValue; VARIANT_BOOL vbSpecified; m_pElem->QueryInterface(IID_IHTMLDOMNode, (void**)&pElemDN); pElemDN->get_attributes(&pACDisp); pACDisp->QueryInterface(IID_IHTMLAttributeCollection, (void**)&pAttrColl); pAttrColl->get_length(&lACLength); vACIndex.vt = VT_I4; for (int i = 0; i < lACLength; i++) { vACIndex.lVal = i; pAttrColl->item(&vACIndex, &pItemDisp); pItemDisp->QueryInterface(IID_IHTMLDOMAttribute, (void**)&pItem); pItem->get_specified(&vbSpecified); pItem->get_nodeName(&bstrName); pItem->get_nodeValue(&vValue); pItemDisp->Release(); pItem->Release(); } pElemDN->Release(); pACDisp->Release(); pAttrColl->Release();
See also
