Detect when user presses ENTER

Asked

Viewed 355 times

6

I have a jQuery code that detects when the key ENTER is pressed, but the event is also called when I use Shift + ENTER, for example.

How do I detect only the ENTER? The intention is to send the form only when the key ENTER is pressed, leaving the Shift + Enter for line breaking.

$(document).on('keyup', '.comment-textarea', function(event) {   
    if (event.which == 13) {
        console.log('ENTER');
    }
});

1 answer

8


Do like, you check if the shift is tight.

$(document).on('keyup', '.comment-textarea', function(event) {   
    if (event.which == 13) {
         if(event.shiftKey){
             console.log('Quebra de linha');
         }else{
             console.log('ENTER');
        }
    }
});
  • Thank you very much, Ricardo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.