Search code examples
jquerylivekeypress

jQuery: how to capture keypress key using live()


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.


Solution

  • Try it with .keyCode instead of .which:

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

    Seem to work ;)