jQuery: Prevent enter key

James T picture James T · Jan 21, 2011 · Viewed 88.3k times · Source

I am trying to prevent the enter key from being put into a textarea, but it doesn't seem to work.

$('#comment').keyup(function(event) {
  if (event.text.charCodeAt() == '10') {
     event.preventDefault();
   }
});

Answer

kajo picture kajo · Jan 21, 2011

I have written little demonstration on jsfiddle.net, where you can try this code

Everybody has right answer :)

$('#comment').keypress(function (event) {
    if (event.keyCode === 10 || event.keyCode === 13) {
        event.preventDefault();
    }
});