3

How do i force a comment by pressing enter? At the moment when i press enter it builds a new line. I've seen quite a few articles here on SO but I can't seem to figure it out.

Here's what I've tried:

function enterForm(event) {
     if(event.keyCode == 13) {
          runForm();
     }
}
function enterForm(event) {
     if(event.keyCode == 13) {
          runForm();
     }
}  

Adding onKeyUp=”enterForm(event)” in the textarea tag. Didn't work.

I also tried something like this:

$('.cmnt_new').on('keyup', function(e) {
    if (e.which == 13 && ! e.shiftKey) {
        // your AJAX call
    }
});

cooking good
  • 1,416
  • 1
  • 14
  • 17
  • Use `textbox` instead of `textarea` or add `javascript` handler in the `textarea`, didn't read your code tho. – The Alpha Nov 04 '13 at 20:23
  • Dumb it down. Your event binding works: http://jsfiddle.net/Tentonaxe/5JKy8/ the problem must be elsewhere. Also, this is a terrible thing to do to your UX. – Kevin B Nov 04 '13 at 20:23
  • The first example you show should work. The jQuery example with `$('.cmnt_new').on...` would work for any elements with `class="mnt_new"` _that already exist when that JS is run._ But you are creating the textarea dynamically, and you don't give it that class. – nnnnnn Nov 04 '13 at 20:25
  • Thanks guys, I noticed it was working in the fiddle but i can't get it to work on my page. @KevinB, I tried making the change to textbox and my textarea disappeared completely. Still stuck. – cooking good Nov 04 '13 at 20:49
  • @nnnnnn Can you please elaborate on this? I'm still stuck. – cooking good Nov 04 '13 at 21:52
  • '; I just realized my submit button was targeting the wrong button and my actual button that i need to target is just a styled div... can anyone please help. i tried using the comment function in place of the this.form.submit... like so: this.form.add_comment(); no avail. – cooking good Nov 05 '13 at 00:46

1 Answers1

3

try keypress as found here:

Submitting a form on 'Enter' with jQuery?

$('.input').keypress(function(e) {
    if(e.which == 13) {
        jQuery(this).blur();
        jQuery('#submit').focus().click();
    }
});
Community
  • 1
  • 1
spojam
  • 164
  • 1
  • 3
  • 13
  • The javascript works sort of but it's not actually processing my data.'; – cooking good Nov 05 '13 at 00:09
  • I just realized my submit button was targeting the wrong button and my actual button that i need to target is just a styled div... can anyone please help. i tried using the comment function in place of the this.form.submit... like so: this.form.add_comment(); no avail. – cooking good Nov 05 '13 at 00:46