This page is In Progress

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

item

Method of dom/HTMLElementdom/HTMLElement

Syntax

var object = object.item(name, index);

Parameters

name

Data-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

Data-type
VARIANT

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

Return Value

Returns an object of type DOM NodeDOM Node

Object

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>

Notes

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.

Syntax

Standards information

Attributions