Skip to content

Instantly share code, notes, and snippets.

@mikehibm
Last active December 13, 2015 18:48
Show Gist options
  • Save mikehibm/4957443 to your computer and use it in GitHub Desktop.
Save mikehibm/4957443 to your computer and use it in GitHub Desktop.
Move focus with Enter key in a form. Move backward with Shift+Enter too.
$(function(){
var elements = "input[type=text]";
$(elements).keypress(function(e) {
var c = e.which ? e.which : e.keyCode;
if (c == 13) {
var index = $(elements).index(this);
var criteria = e.shiftKey ? ":lt(" + index + "):last" : ":gt(" + index + "):first";
$(elements + criteria).focus();
e.preventDefault();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment