IHTMLElementCollection::item Method

Retrieves a form object or an object from an elements collection.


    VARIANT name,
    VARIANT index,
    IDispatch **pdisp


  • name
    [in] VARIANT of type VT_I4 or VT_BSTR that specifies the object or collection to retrieve. If this parameter is an integer, it is the zero-based index of the object. If this parameter is a string, all objects with matching name or id properties are retrieved, and a collection is returned if more than one match is made.
  • index
    [in] VARIANT of type VT_I4 that specifies the zero-based index of the object to retrieve when a collection is returned.
  • pdisp
    [out, retval] The address of a pointer that receives an IDispatch interface for the object or collection if successful, or NULL otherwise.

Return Value

Returns S_OK if successful, or an error value otherwise.


Note   T his method indexes collections by the name or id property of the object; this is a known standards-compliance issue. For interoperability with other browsers, do not referenc e an object by name using this method.

Windows Internet Explorer 8 and later. In IE8 Standards mode, the index parameter is not used. For more information, see Defining Document Compatibility.

Call QueryInterface on the IDispatch interface pointer retrieved to pdisp to obtain the correct interface for the object or collection.

This function returns S_OK even if the element is not found. You should check the value of the pdisp ( IDispatch) pointer returned by this call. If the value of the pointer is NULL, the element was not found and the call was not successful.


The following general example shows how to use the IHTMLSelectElement::length property and item method to iterate through the option objects within a select object.

HRESULT GetOptions(IHTMLSelectElement *ppvSelect, BSTR *pszOptText, long *plItems)
    IDispatch *ppvdispOption;
    IHTMLOptionElement *ppvOption;
    HRESULT hResult;

    // Obtain the number of option objects in the select object.
    for (long i=0;i<*plItems;i++){

        // Get an IDispatch interface for the next option.
        _variant_t index = i;
        hResult = ppvSelect->item( index, index, &ppvdispOption );
        if FAILED(hResult) return(hResult);

        // Query for the IHTMLOptionElement interface.
        hResult = ppvdispOption->QueryInterface( IID_IHTMLOptionElement, 
                                                 (void **) &ppvOption);
        if FAILED(hResult) return(hResult);

        // Add the option text to a list box.
        hResult = ppvOption->get_text(&(pszOptText[i]));
        if FAILED(hResult) return(hResult);
    return S_OK;