Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textarea newline prevention on Enter (jQuery)

I have:

$('#myTextArea').keyup(function(e) {
     if(e.keyCode == 13) {
       e.preventDefault(); // Makes no difference
     $(this).parent().submit(); // Submit form it belongs to
   }
});

How do I prevent the newline that shows up right before form submission? preventDefault() and stopPropagation don't work. I could manually reset the field text, but...

like image 305
atp Avatar asked Jul 13 '10 20:07

atp


1 Answers

This happens because keyup is called after the text has been inserted in the text area.

To catch the key before being entered you need to listen for the keydown event.

So just change the keyup to keydown and you should be good to go.

$('#myTextArea').keydown(function(e) {
like image 80
Gabriele Petrioli Avatar answered Sep 19 '22 15:09

Gabriele Petrioli