4

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?

Brian
  • 349
  • 1
  • 5
  • 16
  • I supposed the key code is 8 when input is fill... but how to get the key press when input is empty? – Brian Sep 03 '13 at 15:41
  • [This works for me](http://jsfiddle.net/C94YB/) on my Galaxy S3. I'm not sure what the problem is regarding your code. As a side note, the `.bind()` method has been deprecated in jQuery 1.7+ in favor of `.on()`. – Ohgodwhy Sep 03 '13 at 15:44
  • Works for me too if the input has "something" to delete on... but doesn't work when input is empty... I use bind method because the fields are created dinamically. But bind method it's not the problem I'm sure of that. – Brian Sep 03 '13 at 15:49
  • Same error, only when input is empty. No solution? – eMarine May 19 '14 at 09:27

2 Answers2

3

input change event

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

backspace

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

Another solution can be check for the length of the characters before, and after, if it has less than one character than before, then clearly it was a backspace that fired, in my case I was using only one input with one character and needed to go back to the previous input after hitting a backspace, for my case I only got also 0 and 229 within the android keyboard, so I did this:

$('input').on('keydown', function (e) {
    switch(e.keyCode){
        case 229:
        case 0:
             if($(this).val() == '')
                 $(this).prev().focus();
             break;
    }
});

The same can be achieved by checking the characters, of course this will only be in the case of a backspace in the android keyboard device, is just a workaround, hope it helps.

Leo
  • 956
  • 8
  • 24