David
David

Reputation: 119

is there a mobile friendly way to detect enter key presses in textarea?

I want to detect when the enter key is pressed in a textarea

I want it to work on mobile too

When the textarea is focused, and they press enter on the onscreen keyboard, it still detects and when they are using an external keyboard or computer keyboard it also detects.

Upvotes: 6

Views: 1674

Answers (1)

Unmitigated
Unmitigated

Reputation: 89344

You can directly check the keyCode.

document.querySelector('textarea').addEventListener('keypress', function(e){
    if(e.keyCode === 13) alert('Enter');
});

Upvotes: 3

Related Questions