Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing the backspace key from moving to the previous page

.keydown(function(e){
        alert(e.keyCode);
...

This does return 8 as key of 'backspace' button. The problem though is the browsers sends be back in history!

This is the most annoying behaviour of windows browsers, I'm just trying to clear List box values with delete and backspace keys. I hate this behavior, and it's exactly the same in IE and in Chrome.

Any ideas I could make use of the backspace key?

like image 218
Anonymous Avatar asked Dec 17 '22 02:12

Anonymous


1 Answers

Just use preventDefault():

$(document).keydown(function(e){
    if ( e.keyCode == 8 ) e.preventDefault();
});

Note however that this will prevent the backspace to work in input fields. This could easily be remedied with the following:

$(document).keydown(function(e){
    if ( e.keyCode == 8 && e.target.tagName != 'INPUT' && e.target.tagName != 'TEXTAREA') {
        e.preventDefault();
    }
});
like image 164
Joseph Silber Avatar answered Jan 31 '23 07:01

Joseph Silber