Search code examples
javascriptjqueryhtmlkeypressforms

jQuery: Prevent enter key


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();
   }
});

Solution

  • 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();
        }
    });