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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint