From Box One to Box Two
The first two text boxes are named "burns" and "tammy". You just learned that if you then attach focus() to the end of the hierarchy statement, you can set focus upon the form element. Let' put those two concepts to work.
The text box "burns" has its onKeyUp Event Handler set to a function named check(). It looks like this:
function check()
{
var letters = document.joe.burns.value.length +1;
if (letters <= 4)
{document.joe.burns.focus()}
else
{document.joe.tammy.focus()}
}
The function first sets the number of characters found within the first textbox (burns) to a variable and then adds one. The onKeyUp Event Handler is the trick. It forces the function to run every time something is entered.
Next, an If statement asks if the number characters within the text box "burns" is less-than or equal to four. If so, then focus remains upon the textbox "burns".
We want the effect to allow four letters to appear in the text box. To get that effect, we add one to the number of characters found in the box. That way, one equals two, two equals three, etc., etc. Once you put in the fourth letter, the If statement is no longer true. Yes, you could get the same effect by not adding one and setting all the numbers up one, but that gets confusing. By adding one, you can set the If statement to the number of letters you want to appear in the box. It's much easier to follow.
Once the If statement is no longer true ("letters" now equals five but only four letters are in the box), focus jumps to the next box, "tammy".
Now that focus is upon "tammy", the function check2() is not the one that runs each time a key is let up. See that in the form elements? The textbox "tammy" has onKeyUp set to check2().
The function looks like this:
function check2()
{
var letters2 = document.joe.tammy.value.length +1;
if (letters2 <= 4)
{document.joe.tammy.focus()}
else
{document.joe.chloe.focus()}
}
Does it look familiar? It should. It's the exact same format as before except for:
- The function name has changed from check() to check2()
- The variable name letters has been changed to letters2
- The hierarchy statements are now set to jump from textbox 2 to 3.
The reason for the changes is that you cannot have the same variable names used twice to represent two different values within the same script.
Once focus is upon textbox three, "chloe", the function check3() takes over and performs the same tasks.
Once focus is upon the fourth textbox, "mardi", the function check4() takes over and does the same thing, except this time when four letters are entered, focus will jump to the submit button.
Don't expect the submit button to do anything. It won't.
Got it?
Do you see how the functions work? Each is doing the exact same thing. You just have to change out some variable names so that you won't get JavaScript errors.
Now, what about getting the form to submit when the user finishes the last box?
Submitting onFocus
Right up front, let me tell you a couple of drawbacks to using this:
- If the user doesn't fill out the form in order, this might submit an unfinished form.
- The effect STINKS if you use a simple mailto: to submit your form. It's not so bad if you submit to a CGI.
Step One: Add an ACTION="--" attribute to your main FORM flag. Like so:
<FORM NAME="joe" ACTION="/Goodies/cgi-bin/form.pl"> Step Two: Lose the submit button altogether.
Step Three: in the final function, set the final focus to:
document.joe.submit()
That will trigger the form to submit just as if you clicked the submit button. Again, if you're using a simple mailto: format, there are only about 100 things that can go wrong. It may not be too bad if you're attaching your form to a CGI.
That's That
This is a great effect that will help your viewers along when filling out your form elements. Remember that you can set the functions to look for any number of characters. Just be sure to set the numbers correctly.
Enjoy!
[The Full Code]
[The Form Elements]
[Getting the First Focus]
[From Box One to Box Two]
[Submitting onFocus]