Gian
Gian

Reputation: 684

How do I trap enter from form except textarea

In a dynamic form, I have the following code to trap 'enter' key.

$(document).bind('keypress', function (e) {  
  if (e.keyCode == 13) { 
    e.preventDefault();
  }
});

Occasionally, there is an element like HTMLTextAreaElement which accept 'enter' key.

how do I unbind preventDefault only for HTMLTextAreaElement.

TIA.

Upvotes: 0

Views: 131

Answers (2)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94369

$("html *:not(textarea)").bind('keypress', function (e) {  
  if (e.keyCode == 13) { 
    e.preventDefault();
  }
});

Demo: http://jsfiddle.net/DerekL/4JWLb/

Upvotes: 0

Ram
Ram

Reputation: 144729

Try this:

if (e.which == 13 && e.target.localName !== 'textarea') { 

Upvotes: 1

Related Questions