0

I want to create a webpage that listens to keypress event for the body to get what the user is typing including backspace for removing the last letter that the user has typed.

So how can I disable the default behaviour of the backspace button (go back) in the browser to achieve that ?

faressoft
  • 19,053
  • 44
  • 104
  • 146

2 Answers2

0

You can disable backspace with keydown event:

$('html').bind('keydown', function (e) {
        if (e.keyCode == 8) {
            return false;
        }
    });
alexey
  • 783
  • 1
  • 7
  • 19
0

i hope this will work for you .

$(document).unbind('keydown').bind('keydown', function (event) {
var doPrevent = false;
if (event.keyCode === 8) {
    var d = event.srcElement || event.target;
    if ((d.tagName.toUpperCase() === 'INPUT' && 
         (
             d.type.toUpperCase() === 'TEXT' ||
             d.type.toUpperCase() === 'PASSWORD' || 
             d.type.toUpperCase() === 'FILE' || 
             d.type.toUpperCase() === 'SEARCH' || 
             d.type.toUpperCase() === 'EMAIL' || 
             d.type.toUpperCase() === 'NUMBER' || 
             d.type.toUpperCase() === 'DATE' )
         ) || 
         d.tagName.toUpperCase() === 'TEXTAREA') {
        doPrevent = d.readOnly || d.disabled;
    }
    else {
        doPrevent = true;
    }
}

if (doPrevent) {
    event.preventDefault();
}

});