jQuery: how to capture keypress key using live()

MacAnthony picture MacAnthony · Jul 22, 2009 · Viewed 23.7k times · Source

I need to capture a tab keypress event on some dynamic inputs, but the normal syntax using the keypress event doesn't seem to be catching the key code.

$('input').live('keypress', function (e) {
   if ( e.which == 9 )
       alert( 'Tab pressed' );
});

This seems to be catching 0 as the keypress when I go through the debugger in firebug no matter which key I press.

Answer

Strae picture Strae · Jul 22, 2009

Try it with .keyCode instead of .which:

$('input').live('keypress', function (e) {
   if ( e.keyCode == 9 ){
       alert( 'Tab pressed' );
    }
});

Seem to work ;)