item method
Retrieves an object from various collections, including the all collection.
![]() |
Syntax
object.item(name, index)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: Object
Returns an object or a collection of objects if successful, or null otherwise.Standards information
- Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5
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 type="text/javascript"> 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 type="text/javascript"> 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 type="text/javascript"> 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 type="text/javascript"> var oItem = document.body.children; if (oItem!=null) { for (i=0; i<oItem.length; i++) console.log(oItem.item(i).tagName); } </script>
See also
- all
- anchors
- applets
- boundElements
- cells
- children
- embeds
- forms
- frames
- images
- links
- rows
- scripts
- tBodies
- window
