I am trying to catpure the enter key from a textarea using javascript. The problem is that although I am able to find out that the "enter" key was pressed, I am not unable to avoid it from coming in the textarea. I dont want the enter key i.e. "\n" to be displayed in the text area.
Any suggestions on how to achieve that?
Thank you.
Try setting this function as the onKeyDown event for the text area:
ex: onkeydown="javascript:return fnIgnoreEnter(event);"
function fnIgnoreEnter(thisEvent) {
if (thisEvent.keyCode == 13) { // enter key
return false; // do nothing
}
}
More recent and much cleaner: use event.key
instead of event.keyCode
. No more arbitrary number codes!
function onEvent(event) {
if (event.key === "Enter") {
return false;
}
};
Mozilla Docs
Supported Browsers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With