1

I have a form which doesn't need a submit button. I want the user to press the ESC key to submit the form.

How can I submit a form using the ESC key?

John Doe
  • 35
  • 7
  • 6
    This sounds like the weirdest UX decision... – Rory McCrossan Jan 05 '16 at 23:11
  • 2
    look at [keyboard events](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent) - but I have to agree with Rory ... `ESC` is probably the last key I would ever choose to use as a submit key! – Jaromanda X Jan 05 '16 at 23:12
  • http://stackoverflow.com/questions/3369593/how-to-detect-escape-key-press-with-javascript-or-jquery – Paul Abbott Jan 05 '16 at 23:13
  • Possible duplicate of [Which keycode for escape key with jQuery](http://stackoverflow.com/questions/1160008/which-keycode-for-escape-key-with-jquery) – technophobia Jan 05 '16 at 23:14
  • @RoryMcCrossan What other key is similar to the enter key? – John Doe Jan 05 '16 at 23:14
  • As strange as it sounds, what you're trying to do could be a legitimate case. Example: A modal form that does not have a save button because it automatically saves - in that case, an "escape" event may also trigger a save event to close the modal. – technophobia Jan 05 '16 at 23:19
  • @technophobia yea, all cool, unless you are a user of this and have no idea that it saved, pulling your hair out finding the submit button – Kamil Gosciminski Jan 05 '16 at 23:47
  • 1
    @ConsiderMe I'm reaching here...work with me ;) – technophobia Jan 05 '16 at 23:48

1 Answers1

2

If its what you want then use the

$(document).keyup(function(e) {
  if (e.keyCode == 27) $('#your_form').submit();
});
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44