item (Form, Elements)

item method

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


object.item(name, index)


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

There are no standards that apply here.


Note   This 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 reference an object by name using this method.

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


The following example uses the item method to retrieve each object in the elements collection of a form object.

<form action="example.asp" method="get">
  First name: <input type="text" name="fname" value="John" /> <br />
  Last name: <input type="text" name="lname" value="Doe" /> <br />
  <input type="submit" value="Submit" />
<script type="text/jscript">
  window.onload = function() {
    var oForm = document.getElementsByTagName("FORM")[0];
    var oColl = oForm.elements;
    var sTypeAttr = new Array();
    var sNameAttr = new Array();
    var sValAttr = new Array();
    for (i=0; i<oColl.length; i++) {     
      sTypeAttr[i] = oColl.item(i).getAttribute("type");
      sNameAttr[i] = oColl.item(i).getAttribute("name");
      sValAttr[i] = oColl.item(i).getAttribute("value");                
    console.log("This form has " + oColl.length + " input elements:" + "\n" +
      "type=" + "\"" + sTypeAttr[0] + "\" name=" + "\"" + sNameAttr[0] + "\" value=" + "\"" + sValAttr[0] + "\"\n" + 
      "type=" + "\"" + sTypeAttr[1] + "\" name=" + "\"" + sNameAttr[1] + "\" value=" + "\"" + sValAttr[1] + "\"\n" +
      "type=" + "\"" + sTypeAttr[2] + "\" name=" + "\"" + sNameAttr[2] + "\" value=" + "\"" + sValAttr[2] + "\"");  

See also




© 2016 Microsoft