jQuery: keyup for TAB-key?
Solution 1:
My hunch is that when you press the tab key, your form's input loses focus, before the keyup happens. Try changing the binding to the body, like this:
$('body').keyup(function(e) {
console.log('keyup called');
var code = e.keyCode || e.which;
if (code == '9') {
alert('Tab pressed');
}
});
Then, if that works (it does for me) try changing the binding to keyDown, instead, and return false. That way you can implement your own custom behavior.
$('.yourSelector').keydown(function(e) {
console.log('keyup called');
var code = e.keyCode || e.which;
if (code == '9') {
alert('Tab pressed');
return false;
}
});
One of these two should work... if it's body, you could attach a keydown handler on body, then if it's tab, find the field with focus (I think there's function .hasFocus()?), and if it's the one you want, proceed and return false. Otherwise, let the browser do its thing.
If you wanted to update the field WITH a tab character, try this in your callback:
var theVal = $(this).val();
theVal = theVal + ' ';
$(this).val(theVal);
Haven't tested it, but it should work. You could also append 3 or 4 spaces instead of the tab character, if it's giving you trouble.
Solution 2:
e = e || window.event;
if(e.keyCode == 9)
alert("Tab pressed");
Try this