item method (Internet Explorer)

Switch View :
ScriptFree
item method

[This documentation is preliminary and is subject to change.]

Retrieves an object from various collections, including the all collection.

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

Syntax

object.item(name, index)

Standards information

Parameters

name [in]

Type: VARIANT

Variant of type Integer or String 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, optional]

Type: VARIANT

Variant of type Integer that specifies the zero-based index of the object to retrieve when a collection is returned.

Return value

Type: ObjectReturns an object or a collection of objects if successful, or null otherwise.

Remarks

The item method cannot retrieve input type=image elements from a form. To access all elements contained in a form, use the children collection.

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

Examples

The following example uses the item method to retrieve each object from the document. In this case, the method parameter is a number, so the objects are retrieved in the order they appear in the document.


<script language="JScript">
var coll = document.all;
if (coll!=null) {
    for (i=0; i<coll.length; i++) 
        console.log(coll.item(i).tagName);
}
</script>

The next example uses the item method to retrieve a collection of all objects in the document that have "Sample" as an ID. The example then uses item again to retrieve each object from the "Sample" collection.


<script language="JScript">
var coll = document.all.item("Sample");
If (coll != null) {
    for (i=0; i<coll.length; i++) {
        console.log(coll.item(i).tagName);
    }
}
</script>

The last example is similar to the previous example, but uses the optional index parameter of item to retrieve individual objects.


<script language="JScript">
var coll = document.all.item("Sample")
if (coll!=null) {
    for (i=0; i<coll.length; i++)
        console.log(document.all.item("Sample",i).tagName);
}
</script>

This example uses the item method to get a collection of the children of the document body.


<script language="JScript">
var oItem = document.body.children;
if (oItem!=null) {
    for (i=0; i<oItem.length; i++) 
        console.log(oItem.item(i).tagName);
}
</script>

 

 

Build date: 3/8/2012