I am trying to fire an event on the right and left arrow key presses with jQuery. Using the following code, I can fire events on any of the alphanumeric keys, but the cursor keys (up, down, left, right) fire nothing. I am developing the site primarily for IE users because it is a line of business app. Am I doing something wrong here?
$('document').keypress(function(e){ switch (e.which) { case 40: alert('down'); break; case 38: alert('up'); break; case 37: alert('left'); break; case 39: alert('right'); break; default: alert('???'); } });
e.which doesn’t work in IE try e.keyCode, also you probably want to use keydown() instead of keypress() if you are targeting IE.
See http://unixpapa.com/js/key.html for more information.