Skip to content

Instantly share code, notes, and snippets.

@takinbo
Created February 25, 2012 19:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takinbo/1910209 to your computer and use it in GitHub Desktop.
Save takinbo/1910209 to your computer and use it in GitHub Desktop.
Form Navigation
// Adds the ability to navigate text boxes while pressing either n or p
$('.bigbox2').keypress(function (e) {
switch (e.which) {
case 78:
case 110:
// N was pressed
el = $(this);
prefix = el.attr('name').replace(/-.*$/, "");
pos = $('input[name|="'+prefix+'"]').index(el);
if (pos < $('input[name|="'+prefix+'"]').length) {
$('input[name|="'+prefix+'"]').eq(pos+1).focus().select();
}
return false;
case 80:
case 112:
// P was pressed
el = $(this);
prefix = el.attr('name').replace(/-.*$/, "");
pos = $('input[name|="'+prefix+'"]').index(el);
if (pos > 0) {
$('input[name|="'+prefix+'"]').eq(pos-1).focus().select();
}
return false;
default:
return true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment