Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created July 20, 2010 17:02
Show Gist options
  • Save jimfleming/483224 to your computer and use it in GitHub Desktop.
Save jimfleming/483224 to your computer and use it in GitHub Desktop.
Auto-select next field as the user (de-)fills them. Useful for three phone number fields.
var $phone1 = $('input#phone1');
var $phone2 = $('input#phone2');
var $phone3 = $('input#phone3');
$phone1.change(function() {
if ($phone1.val().length == 3) // if they've typed 3 characters
$phone2.focus(); // select the next field
});
$phone2.change(function() {
if ($phone2.val().length == 3) // if they've typed 3 characters
$phone3.focus(); // select the next field
else if ($phone2.val().length == 0) // if they backspace to 0 characters
$phone1.focus(); // select the previous field
});
$phone3.change(function() {
if ($phone3.val().length == 0) // if they backspace to 0 characters
$phone2.focus(); // select the previous field
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment