Share via


item method

Retrieves an attribute for an element from an attributes collection.

 

Syntax

 retVal = object.item(name);

Parameters

  • name [in]
    Type: VARIANT

    A pointer to a VARIANT of type VT_I4 or VT_BSTR 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.

Standards information

Remarks

Call QueryInterface on pdisp to obtain an IHTMLDOMAttribute interface pointer.

This method returns an error if the attribute is not found. When the IHTMLAttributeCollection::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 IHTMLDOMAttribute2::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 IHTMLAttributeCollection2::getNamedItem to reference an attribute by IHTMLDOMAttribute2::name. For more information, see Defining Document Compatibility.

Examples

This example uses the IHTMLAttributeCollection::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();