spacer
Yehuda Shiran November 16, 1999
Referencing Forms
Tips: November 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Each <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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business