November 16, 1999 - Referencing Forms
November 16, 1999 Referencing Forms Tips: November 1999
Yehuda Shiran, Ph.D.
|
<FORM>
...</FORM>
tag pair defines one form. A single HTML document can contain several different forms, containing various form elements. JavaScript provides two main ways to reference a form:
document.formName
document.forms[index]
Take a look at the following document:
<HTML>
<HEAD><TITLE>Forms</TITLE></HEAD>
<BODY>
<FORM NAME="firstForm"></FORM>
<FORM></FORM>
<FORM NAME="thirdForm">
<INPUT TYPE="text" NAME="myField" SIZE="30">
</FORM>
</BODY>
</HTML>
The value of the text field in the third form can be retrieved in one of the following fashions:
document.thirdForm.myField.value
document.forms[2].myField.value
Notice that the forms
array (a property of the document
object) is zero-based. In other words, 0 reflects the first form, 1 reflects the second form, and so forth.
Note that the document.forms
object can also be used with the name of the desired form (like a Visual Basic collection):
document.forms.thirdForm
document.forms["thirdForm"]
The forms
array is supported by all JavaScript-enabled browsers, including Navigator 2.0x and Internet Explorer 3.0x.