A Form Object Example

The Form object offers properties and methods for any form on the current page. The Form object also contains an Element object that can reference any control on the form either by array or by name. Elements are intrinsic HTML controls and are discussed in more detail later in this chapter. As an example, Listing 3-5 shows code that accesses all of the controls on a form and changes their values. Figure 3-10 on page 68 shows a sample of the output.

<HTML>
<HEAD>
<TITLE>Forms and Elements</TITLE>

<!—
This demo loops through
all of the controls and
changes their value to
"Changed!" 
—>

<SCRIPT LANGUAGE="VBScript">
<!—
    Sub cmdChange_OnClick
        For i=0 To Document.frmForm.Elements.Count-1
            Document.frmForm.Elements(i).Value="Changed!"
        Next
    End Sub
—>
</SCRIPT>

</HEAD>
<BODY>
<CENTER>

<FORM NAME="frmForm">
<INPUT TYPE="TEXT"><P>
<INPUT TYPE="TEXT"><P>
<INPUT TYPE="TEXT"><P>
<INPUT TYPE="TEXT"><P>
<INPUT TYPE="TEXT"><P>
<INPUT TYPE="BUTTON" NAME="cmdChange"
VALUE="Push and Watch the Controls!"><P>
</FORM>

</CENTER>
</HTML> 

Listing 3-5.

Form and Element objects in VBScript.

Figure 3-10.

Sample output using Form and Element objects.

© 1996 by Scot Hillier. All rights reserved.