Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Last active September 18, 2017 22:13
Show Gist options
  • Save stevenharman/6054478 to your computer and use it in GitHub Desktop.
Save stevenharman/6054478 to your computer and use it in GitHub Desktop.
⌘-ENTER: submit that form!
$(document).on('keydown', 'form :input:not(:disabled)', function(e) {
if(e.keyCode == 13 && (e.metaKey || e.ctrlKey)) {
$form = $(this).parents('form')
if ($form[0].checkValidity()) {
$form.submit();
}
}
});
@jasonkarns
Copy link

Isn't it easier to just make sure there is an input[type=submit] element in the form? Which there should be, anyway, if it's a submit-able form.

[EDIT] After testing this out, I now feel this should be handled by the browser. Currently, plain-ole 'Enter' with any field focused (other than textarea) will submit it for you automatically.

@drabiter
Copy link

And if you don't need the input[type=submit] just hide it.

@stevenharman
Copy link
Author

@jasonkarns Yes, ENTER in non-textarea will work. This makes for a consistent experience no matter what kind of field you're in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment