Created
December 17, 2013 19:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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