Ordinarily working through forms objects using array references is a bit awkward. Have a look at the example below:
// this will extract the entered value in the 3rd element // of the first form on the page var myVariable = document.forms.elements.value;
Rather than keeping track of the order of these elements it is much easier to name the <form> tag and to name the <input> tags it contains. For example if we have this form to work with ...
<form name="mailform" action="#" method="get" id="mailform"> <label for="phone">Telephone:</label> <input name="phone" type="text" /> </form>
// this will extract the entered value in the 3rd element // of the first form on the page var myVariable = document.forms.mailform.phone.value;
This is much easier to read!
Add some data to the telephone entry field below and click the "Check Value" button.
The technique explained above will only work where form fields allow the typing in of text data, so <input type="text" ... /> and <textarea> will work but checkboxes, radio button and select boxes will not.
Here are a list of resources which explain the differing methods of retrieving meaningful data from these other types of controls.
The Document Object Model is covered quite comprehensively by Quirksmode.