Submit a Form on Command + Enter from Textarea with jQuery

I’m a big fan of keyboard control in productivity applications. I love being able to type my changes/additions and submit them without needing to tab around too much, or gasp grab the mouse. I’m not the only one who feels this way. David Walsh has a great post explaining how to submit web forms with Command+Enter. However, for my needs I needed to tweak this a little:

  1. I only want Command+Enter to submit the form if I’m in a textarea.
  2. I need the form submission to play nicely with AJAX requests, specifically when using jQuery delegates to catch the form submission. In David’s example code, the submit() was firing before the delegate could catch the event and preventDefault() on it.

Luckily it was quite simple to adapt his code for my needs.

View the code on Gist.