DHTML Collections
DOM Level 1 Specification.
Retrieves a collection of all control elements in a given FORM.
[ collElements = ] form.elements |
[ oObject = ] form.elements ( vIndex [ , iSubIndex ] ) |
collElements |
Reference to an array of elements contained by the object. |
oObject |
Reference to an individual item in the array of elements contained by the object. |
vIndex |
Required. Number or string specifying the element or collection to retrieve. If this parameter is a number, the method returns the element in the collection at the given position, where the first element has value 0, the second has 1, and so on. If this parameter is a string and there is more than one element with the name or id property equal to the string, the method returns a collection of matching elements. |
iSubIndex |
Optional. Position of an element to retrieve. This parameter is used when vIndex is a string. The method uses the string to construct a collection of all elements that have a name or id equal to the string and then retrieves from this collection the element at the position specified by iSubIndex. |
The elements collection returns an array, in source order, of all the control elements in a specified form.
This collection can contain any combination of INPUT, SELECT, and TEXTAREA objects.
Thus, an element in any given form can be referenced as:
document.formName.elements [ controlIndex ]
document [ 'formName' ].elements [ controlIndex ]
document.forms [ formIndex ].elements [ controlIndex ]
As the collection is zero-based, the first element is elements [ 0
] , the second is elements [ 1
] , and so on.
In addition, an element in a form may also be referenced by its name, and in DOM-compliant browsers, by its id property.
document.formName.elementName
document [ 'formName' ].elementName
document.forms [ formIndex ].elementName
document.getElementById ( 'elementID' )
This example shows how the elements collection is used to retrieve the name of each control element in the form.
<form name="formName" onsubmit="getFormEls ( this )">
<table align="center" ... >
... form control definitions ...
</table>
</form>
<script language="JavaScript">
<!--
function getFormEls ( form ) {
els = form.elements;
html = "";
for ( e = 0; e < els.length; e ++ )
html += els ( e ).name + "\n";
alert ( html );
}
//>
</script>
FORM
forms