how to capture backspace key code in android browser - javascript?

Brian picture Brian · Sep 3, 2013 · Viewed 8k times · Source

I have a page made in pure HTML Javascript... I handle the keyup code and I need to get the key code, when key code == 8 (backspace) special task must be run... but if I open the page in android browser, chrome, or whatever... backspace doesn't return any key code...

I've made:

$( '.input-cell' ).bind( 'keyup', function( e ){

   var keycode =  e.keyCode ? e.keyCode : e.which;

   alert( keycode );

   if( keycode == 8 ) {
               .....
   }
});

The alert returns me all the keycodes but the backspace... is there any way to capture the backspace press event?

Answer

hamze shoae picture hamze shoae · Sep 23, 2014

input change event

$('#myinput').bind('input', function(){
    // code
});

backspace

$('#myinput').on('keypress', function() {
    //code
    }).on('keydown', function(e) {
        if (e.keyCode==8) {
            //code
        }
    });