Java Reference
In-Depth Information
worry about pages containing multiple forms until you have to submit information to a web server — then
you need to be aware that the information from only one of the forms on a page can be submitted to the
server at one time.
To create a form, use the <form> and </form> tags to declare where it starts and where it ends. The
<form/> element has a number of attributes, such as the` action attribute, which determines where the
form is submitted to; the method attribute, which determines how the information is submitted; and
the target attribute, which determines the frame to which the response to the form is loaded.
Generally speaking, for client-side scripting where you have no intention of submitting information to
a server, these attributes are not necessary. They will come into play in a later chapter when you look
at programming server pages. For now the only attribute you need to set in the <form/> element is the
name attribute, so that you can reference the form.
So, to create a blank form, the tags required would look something like this:
<form name=”myForm”>
</form>
You won't be surprised to hear that these tags create a Form object, which you can use to access the
form. You can access this object in two ways.
First, you can access the object directly using its name — in this case document.myForm. Alternatively,
you can access the object through the document object's forms collection property. Remember that
the last chapter included a discussion of the document object's images collection and how you can
manipulate it like any other array. The same applies to the forms collection, except that instead of each
element in the collection holding an IMG object, it now holds a Form object. For example, if it's the fi rst
Form in the page, you reference it using document.forms[0].
Many of the attributes of the <form/> element can be accessed as properties of the Form object. In par-
ticular, the name property of the Form object mirrors the name attribute of the <form/> element.
Try It Out The forms Collection
Let's have a look at an example that uses the forms collection. Here you have a page with three forms
on it. Using the forms collection, you access each Form object in turn and show the value of its name
property in a message box.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 7: Example 1</title>
<script type=”text/javascript”>
function window_onload()
{
var numberForms = document.forms.length;
var formIndex;
for (formIndex = 0; formIndex < numberForms; formIndex++)
{
alert(document.forms[formIndex].name);
}
Search WWH ::




Custom Search