Alex
Alex

Reputation: 68068

keypress - escape doesn't work

$(el).bind('blur keypress', function(event){


  if(event.type == 'keypress' && event.keyCode != 13) return;
  alert(1);

});

I get the alert box even when I press escape, and I shouldn;t because I only allowed Enter...

Upvotes: 2

Views: 389

Answers (2)

ShankarSangoli
ShankarSangoli

Reputation: 69915

If you want to only allow Enter then try this. Check for true condition it will make more clear to you. Also instead of e.keyCode you can use e.which, it is abstracted by jQuery to make it corss browser compatible.

$(el).bind('blur keypress', function(event){
  if(event.type == 'keypress' && event.which == 13){
      alert(1);
  }
});

Upvotes: 1

AmGates
AmGates

Reputation: 2123

By default "return" will send the called function the value true, so in this case the default event is allowed to be performed so whatever button u press u will get the alert. So change the return statement as return false

Upvotes: 3

Related Questions