jquery – Detect when user presses ENTER

Question:

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

How do I detect just the ENTER ? The intention is to submit the form only when the ENTER key is pressed, leaving Shift + Enter for newline.

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

Answer:

Do this, 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');
        }
    }
});
Scroll to Top