Skip to content

Instantly share code, notes, and snippets.

@macu
Created December 17, 2013 19:30
Show Gist options
  • Select an option

  • Save macu/8011129 to your computer and use it in GitHub Desktop.

Select an option

Save macu/8011129 to your computer and use it in GitHub Desktop.
Prevent single-input forms from submitting when the user hits Enter, unless the input includes the `submit13` class.
// prevent all forms from submitting on pressing Enter in an input
// unless the input specifies the `submit13` class.
$("input").on("keypress", function(e) {
if (e.which == 13) {
// prevent all first, and trigger manually to bypass the quirk
// where browsers will submit single-input forms on pressing Enter.
// otherwise two submit events would be generated in quirk cases.
e.preventDefault();
if ($(this).is(".submit13")) {
$(this).closest("form").trigger("submit");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment