David Lohmeyer's Blog

Disabling the backspace key on a page with jQuery

I had an issue with a multi-page form recently where Internet Explorer would go back a page immediately upon hitting the backspace key.  If you accidentally do this while not in a text field you'll lose your form data.  Other browsers actually ask you if you want to resend data when you go back.  The solution was to disable the backspace key on my form page.  I accomplished this with the following Javascript (requires jQuery):

$(document).keydown(function(e) {
var element = e.target.nodeName.toLowerCase();
if (element != 'input' && element != 'textarea') {
    if (e.keyCode === 8) {
        return false;
    }
}
});

Your backspace key will still work while typing in textarea and input form elements.

Related Posts