I wonder what I'm doing wrong:
$('.s').keypress(function(e) {
switch (e.keyCode) {
case 8: // Backspace
//console.log('backspace');
case 9: // Tab
case 13: // Enter
case 37: // Left
case 38: // Up
case 39: // Right
case 40: // Down
break;
default:
doSearch();
}
});
I want my doSearch()
function also to be fired when I hit the Backspace key. At the moment absolutely nothing happens when I press Backspace in Chrome and Safari.
any ideas?
According to the jQuery documentation for .keypress(), it does not catch non-printable characters, so backspace will not work on keypress, but it is caught in keydown and keyup:
In some instances keyup isn't desired or has other undesirable effects and keydown is sufficient, so one way to handle this is to use
keydown
to catch all keystrokes then set a timeout of a short interval so that the key is entered, then do processing in there after.If you want to fire the event only on changes of your input use:
I came across this myself. I used
.on
so it looks a bit different but I did this:Adding my Work Around here. I needed to delete ssn typed by user so i did this in jQuery
Use
keyup
instead ofkeypress
. This gets all the key codes when the user presses something