Accessing the form elements
The form validation script needs to access the form in the HTML page to determine
what values the user has filled in. So first we have to enter the form by means of the Level 0 DOM.
The general syntax for accessing a form element is:
The first elements is elements, the second elements etc. Every <input>, <select> and <textarea> is an element.
In some cases, it’s better to use the names of the forms and elements. In HTML, you have to give a name to each form and each element, like:
<form name=”personal” action=”something.pl” onsubmit=”return checkscript()”>
<input type=text size=20 name=name><input type=text size=20 name=address>
<input type=text size=20 name=city>
Now you can access these elements by:
The advantage of using names is that you can put all elements somewhere else in the page and still have a working script, while a script using numbers will have to be changed. After all, the input box city is document.forms.elements in the example above, but when you suddenly put it at the top of the form, it becomes document.forms.elements and you have to change the script.
When you start writing your own scripts using the code snippets below, it is always very important to know exactly how your form is built. To help you, I wrote a form printing script that
prints out the form structure for you.