I am trying the following code for triggering a js method when space-bar
is pressed within an input box.
$(\'#j1
In non-IE browsers, you want the which
or charCode
property in a keypress
event rather than the keyCode
property. The keypress
event is for detecting typed characters while keyup
and keydown
are for detcting physical keys (in those events, keyCode
works in every major browser).
var charCode = (typeof event.which == "number") ? event.which : event.keyCode;
However, jQuery normalizes the which
property of keypress events by using code similar to this, so in jQuery you just need
var charCode = event.which;
For (a lot) more detail about key events, see http://unixpapa.com/js/key.html.