Automatically move cursor into next field using javascript

This is a little script which will move the focus to the next control when the current textbox hits its maximum length. It also handles the strange(or expected?) behaviour whereby pressing <Tab> would ‘jump’ two controls.

Code Snippet
  1. function moveOnMax(event, field, nextFieldID) {
  2.         event = event || window.event;
  3.         if (event.keyCode != 9) {
  4.             if (field.value.length >= field.maxLength) {
  5.                 nextFieldID.focus();
  6.             }
  7.         }
  8.     }

You can wire this into an ASP textbox as:

Code Snippet
  1. <asp:TextBox ID=”TextBox1″ ClientIDMode=”Static” MaxLength=”1″ onkeyup=”moveOnMax(event, this, document.getElementById(‘TextBox2’))” Width=”20″ Height=”20″ runat=”server”></asp:TextBox>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s