Java Reference
In-Depth Information
Each form element inside the web page is made available to you as—yes, you guessed it—an object.
As with all the other objects you have seen, each element's object has its own set of distinctive
properties, methods, and events. You'll be taking a look at each form element in turn and how to
use its particular properties, methods, and events, but before you do that, let's look at properties
and methods that the objects of the form elements have in common.
Common properties and methods
Because most form elements are created by the
<input/>
element, it would be correct to guess that all
form elements share several properties and methods in common.
Here are a few.
The name property
One property that all the objects of the form elements have in common is the
name
property. You
can use the value of this property to reference that particular element in your script. Also, if you are
sending the information in the form to a server, the element's
name
property is sent along with any
value of the form element, so that the server knows what the value relates to.
The value property
Most form element objects also have the
value
property, which returns the value of the element. For
example, for a text box, the
value
property returns the text that the user entered in the text box.
Also, setting the value of the
value
property enables you to put text inside the text box. However,
the use of the
value
property is specific to each element, so you'll look at what it means as you look
at each individual element.
The form property
All form element objects also have the
form
property, which returns the
Form
object in which the
element is contained. This can be useful in cases where you have a generic routine that checks the
validity of data in a form. For example, when the user clicks a Submit button, you can pass the
Form
object referenced by the
form
property of the Submit button to your data checker, which can use it to
loop through each element on the form in turn, checking that the data in the element is valid. This is
handy if you have more than one form defined on the page or where you have a generic data checker
that you cut and paste to different pages—this way you don't need to know the form's name in advance.
The type property
Sometimes it's useful to know what type of element you're dealing with, particularly where you're
looping through the elements in a form using the
elements
collection property. This information
can be retrieved by means of the
type
property, which each element's object has. This property
returns the type of the element (for example,
button
or
text
).
The focus() and blur() Methods
All form element objects also have the
focus()
and
blur()
methods.
Focus
is a concept you might
not have come across yet. If an element is the center of the focus, any key presses made by the user