Other Form Object Properties and Methods - Page 4
September 21, 2001
The HTML elements commonly found in forms, which we will look at
in more detail shortly, also have corresponding objects. One way
of accessing these is through the elements[] property of the Form
object. This is an array just like the forms[] array property of
the document object that we have just seen. The elements[] array
contains all the objects corresponding to the HTML interaction
elements within the form, with the exception of the little used
<INPUT TYPE=image> element. As we'll see later, this
property is very useful for looping through each of the elements
in a form. For example, we could loop through each element
checking that it contains valid data prior to submitting the
form.
Being an array, the elements[] property of the Form object has
the length property, which tells us how many elements are in the
form. The Form object also has the length property, which also
gives us the number of elements in the form. Which of these you
use is up to you since both do the same job, although writing
document.myForm.length is shorter, and so quicker to type and
less lengthy to look at in code, than
document.myForm.elements.length.
When we submit data from a form to a server, we normally use the
submit button, which we will come to shortly. However, the Form
object also has the submit() method which does nearly the same
thing. It differs in Netscape Navigator since it does not call
the onsubmit event handler for the submit event of the Form
object.
Recall that in the last chapter we saw how return values passed
back from an event handler's code can affect whether the normal
course of events continues or is cancelled. We saw, for example,
that returning false from a hyperlink's onclick event handler
causes the link's navigation to be cancelled. Well, the same
principle applies to the Form object's onsubmit event handler,
which fires when the user submits the form. If we return true to
this event handler, then the form submission goes ahead; if we
return false then the submission is cancelled. This makes the
onsubmit event handler's code a great place to do form
validation; checking that what the user has entered into the form
is valid. For example, if we ask for their age and they enter
"mind your own business" we can spot that this is text
rather than a valid number, and stop them from continuing. We'll
see this in action when we look at server-side scripting in
Chapter 15.
As well as there being a reset button, which we will discuss
later in the chapter, theForm object has the reset() method,
which clears the form, or restores default values if these exist.
Creating blank forms is not exactly exciting or useful, so now
let's turn our attention to the HTML elements that provide
interaction functionality inside our forms.
HTML Elements in Forms
There are about ten elements commonly found within <FORM>
elements. The most useful are shown below, ordered into general
types. We give each its name and, in parentheses, the HTML needed
to create it, though note this is not the full HTML but only a
portion.
As you can see, most of the form elements are created using the
<INPUT> tag. One of the <INPUT> tag's attributes is
the TYPE attribute. It's this attribute that decides which of the
HTML elements this will be. Examples of values for this attribute
include button, to create a button, and text to create a text
box.
Each form element inside the web page is made available to us as,
yes, you guessed it, an object. As with all the other objects we
have seen, each element's object has its own set of distinctive
properties, methods and events. We'll be taking a look at each
form element in turn and how to use its particular properties,
methods, and events. But, before we do that, let's look at
properties and methods that the objects of the form elements have
in common.
Try It Out: The Forms Array - Page 3
Beginning JavaScript
Common Properties and Methods - Page 5
|