This has been a tutorial that's been sitting in my "working" directory for quite some time. My "working" file is place where I have little blips of code that could, at sometime or another, become a tutorial. Most of the pieces of code don't work. That's why they're in a working directory.
This concept always seemed like something that I could do, but I could never quite get it until I posted the code to the HTML Goodies Discussion groups. Thanks to Jim Young for helping. He reposted a code he created for a client that got the same effect I'll show you here. I didn't use his full code, just the one missing piece that I needed. Thank you Jim - you're a gentleman.
The Effect
I am often asked how to set up a form so that after the user fills in a pre-determined number of characters, the cursor jumps to the next form element all by itself.
Please understand that this only works with browsers version 4.0 and above. I'll tell you why in a moment. But don't let that stop you from installing this on your forms. Lower version browsers will not understand the Event Handler command and will not trigger the functions. That means no errors. Those that have browsers version 4.0 or higher get the effect, those who don't just fill in the form as they normally would.
Here's an example with four text boxes:
The Form Elements
<FORM NAME="joe"> <INPUT TYPE="text" name="burns" size="10" onKeyUp="check()"><BR> <INPUT TYPE="text" name="tammy" size="10" onKeyUp="check2()"><BR> <INPUT TYPE="text" name="chloe" size="10" onKeyUp="check3()"><BR> <INPUT TYPE="text" name="mardi" size="10" onKeyUp="check4()"><BR> <INPUT TYPE="submit" VALUE="Click to Send" NAME="go"> </FORM> |
This is a basic form. The name of the form is "joe".
After that, four text boxes. Each box is given a name, a size, a maximum length, and is finally attached to a function triggered to work using the onKeyUp Event Handler.
This is why the script only works in version 4.0 or better browsers. That onKeyUp Event Handler is a relatively new command. This is where I ran into nothing but trouble. You see, the script must be set up so that every time someone enters a new character into a text box, the function must run in order to recount the number of characters the user has entered.
I tried everything - I mean everything. I went with onFocus, onClick, I tried setting the function to a "for" loop and a "while" loop. I set up a setTimeout() function. Nothing worked. As soon as I put in the onKeyUp, success. Each time a key is released, the function runs.
So easy, yet so hard to find.
Now that we know the names of all of the form elements, we can start to build some JavaScript hierarchy statements. For example, the first text box can be called upon through the statement document.joe.burns.
Furthermore, the number of characters within the first text box can be returned through the hierarchy statement document.joe.burns.value.
With those two concepts, the scripts basically build themselves.
You may have noticed that when you arrived at this page, the first text box already had the cursor blinking away. That didn't just happen by default. I needed to "force" focus to the first text box. I did it in the BODY flag like so:
It's that focus() that does the trick. By attaching it to the end of a hierarchy statement, it puts focus upon the form element. In this case, the first text box (burns) under the form (joe). Getting the First Focus
<BODY BGCOLOR="ffffff" onLoad="document.joe.burns.focus()">