Java Reference
In-Depth Information
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 ele-
ment on the form in turn, checking that the data in the element are valid. This is handy if you have more
than one form defi ned 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 will be
passed directly to that element. For example, if a text box has focus, pressing keys will enter values into
the text box. Also, if a button has the focus, pressing the Enter key will cause the button's onclick event
handler code to fi re, just as if a user had clicked the button with his mouse.
The user can set which element currently has the focus by clicking it or by using the Tab key to select
it. However, you as the programmer can also decide which element has the focus by using the form ele-
me nt 's o b j e c t 's focus() method. For example, if you have a text box for the user to enter his age and he
enters an invalid value, such as a letter rather than a number, you can tell him that his input is invalid
and send him back to that text box to correct his mistake.
Blur , which perhaps could be better called “lost focus,” is the opposite of focus. If you want to remove
a form element from being the focus of the user's attention, you can use the blur() method. When used
with a form element, the blur() method usually results in the focus shifting to the page containing
the form.
In addition to the focus() and blur() methods, all the form element's objects have the onfocus and
onblur event handlers. These are fi red, as you'd expect, when an element gets or loses the focus, respec-
tively, due to user action or the focus() and blur() methods. The onblur event handler can be a good
place to check the validity of data in the element that has just lost the focus. If the data are invalid, you
can set the focus back to the element and let the user know why the data he entered are wrong.
Remember that the submit() method behaves differently than focus() and blur() in that it does
not fi re the submit event and onsubmit event handler.
One thing to be careful of is using the focus() and blur() methods in the onfocus or onblur event
handler code. There is the danger of an infi nite loop occurring. For example, consider two elements,
each of whose onfocus events passes the focus to the other element. Then, if one element gets the
focus, its onfocus event will pass the focus to the second element, whose onfocus event will pass the
focus back to the fi rst element, and so on until the only way out is to close the browser down. This is not
likely to please your users!
Also be very wary of using the focus() and blur() methods to put focus back in a problem fi eld if
that fi eld or others depend on some of the user's input. For example, say you have two text boxes: one
Search WWH ::




Custom Search