0

My Javascript:

$("#test").keypress(function(e){
       if (document.all){ var evt = event.keyCode; }
       else if(e.which) { var evt = e.which;       }           
       else             { var evt = e.charCode;    }

       if (evt == 13){ // with any other key, the alert dos not fire
            alert(evt);
       }
       return true;
});

Jsfiddle demo

The keycode 13 Enter fires the alert, but any other dosent.

Can any one tell me why?

I need to verify if the 13 or 9 tab was fired.

Thanks.

Nic
  • 13,287
  • 7
  • 40
  • 42
Ricardo Binns
  • 3,228
  • 6
  • 44
  • 71

2 Answers2

2

Use keydown rather than keypress

$('#test').live('keydown', function(e) { 
    var k = e.keyCode || e.which; 

    if (k == 9 || k == 13) { 
        e.preventDefault();
        alert(k);
    } 
});

working: http://jsfiddle.net/hunter/cDVek/15/

hunter
  • 62,308
  • 19
  • 113
  • 113
0

add a conditional clause to check for evt == 9:

if (evt == 13) { 
   alert(evt); 
} else if (evt == 9) { 
   alert(evt);
}
defvol
  • 14,392
  • 2
  • 22
  • 32