glimpse nirvana
glimpse nirvana

Reputation: 321

How to use jquery to bind ctrl+enter to ajax form submission

The following code will submit an ajax form when the user hits ctrl+enter while in the feedback input area. It works fine - but only once. I need to bind this function to the comment form so it persists and allows multiple submissions. In other words - the form is cleared and represented to the user after each submission. However, the following code only works for the first submission and thus ctrl+enter doesn't work for the second submission.

$('#comment_body').keydown(function(e) {
  if (e.ctrlKey && e.keyCode === 13) {
    return $('#comment_submit').trigger('submit');
  }
});

I've tried .live and .bind but can't get the syntax right to allow resubmission.

Thanks

Upvotes: 5

Views: 2882

Answers (2)

glimpse nirvana
glimpse nirvana

Reputation: 321

This does it. I need .live to get it to persist for future events. I just got the syntax wrong multiple times.

$('#comment_body').live('keydown', function(e) {
  if (e.ctrlKey && e.keyCode === 13) {
    $('#comment_submit').trigger('submit');
  }
});

Upvotes: 6

Hardik
Hardik

Reputation: 536

you are using an id selector and if it's comments chances are same div's will be created with multiple id's and that could be the reason that it gets executed only once.

Upvotes: 0

Related Questions