0

Here is my code:

$("input").on({
    keydown: function(ev) {
        if (ev.which === 27){   // esc button
           backspace_emolator();
        } else if (ev.which === 8){   // backspace button
           console.log('backspace button pressed');
        }
    }
});

function backspace_emolator(){
   // remove one character exactly how backspace button does
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="fname" class="myinput">

All I'm trying to do is making esc button exactly the same as backspace button. So I want to remove one character of .myinput's value when <kbd>esc</kbd> is pressed.

How can I do that?


Note: I don't want to just remove one character of .myinput's value. I want to press backspace button. So backspace button pressed should be shown in the console when I press esc button.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • 1
    Possible duplicate of [Is it possible to simulate key press events programatically?](http://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programatically) – Ben Fortune Jan 20 '17 at 11:05

3 Answers3

1

This is what I would do. I do not argue that this is the best solution, instead it is a working one.

$("input").on({
    keydown: function(ev) {
        if (ev.which === 27){   // esc button
          backspace_emolator(this);
        } else if (ev.which === 8){   // backspace button
          console.log('backspace button pressed');
        }
    }
});

function backspace_emolator(el){
   // remove one character exactly how backspace button does
   var val = el.value
   el.value = val.substring(0, val.length - 1);
   // trigger event
   var e = jQuery.Event("keydown");
   e.which = 8; // # Backspace keyCode
   $(el).trigger(e);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="fname" class="myinput">

Edited: I have modified my code in respect to your edit. The reason @Alexandru-Ionut Mihai suggestion does not work for you is because you are only listening for keydown and in his example he is triggering keypress.

Donii Hoho
  • 371
  • 1
  • 6
  • That is correct kinda .. upvote, but I've edited my question, please take a look at it. – Martin AJ Jan 20 '17 at 10:44
  • I have edited my answer and tested it, Both esc and backspace now trigger the console.log and both are clearing one character. – Donii Hoho Jan 20 '17 at 12:25
  • Your fiddle works exactly as expected, but I don't know why it doesn't work on my real codes `:-(` – Martin AJ Jan 20 '17 at 12:28
  • 1
    From your screenshot it seems to me that it has nothing to do with the trigger and everything to do with the arguments you are passing to the regexp replace function. Have a console.log output just before the argument is passed to regexp replace, my wild guess is that it is too big. Here are some registered cases: [link](http://stackoverflow.com/questions/22123769/rangeerror-maximum-call-stack-size-exceeded-why) – Donii Hoho Jan 20 '17 at 12:43
1

Here is solution:

You have to trigger an event for your input.

$("input").on({
    keydown: function(ev) {
        if (ev.which === 27){   // esc button
          backspace_emolator();
        }
    }
});

function backspace_emolator(){
  var e = jQuery.Event("keydown", { keyCode: 8 }); // you can specify your desired key code 
  $('input').trigger(e);
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

Use this code it works as you want it to be.

$("input").on({
    keydown: function(ev) {
        if (ev.which === 27){   // esc button
           backspace_emolator(ev);
        }
        if (ev.which === 8){   // backspace button
           console.log('backspace button pressed');
        }
    }
});

function backspace_emolator(ev){
   // remove one character exactly how backspace button does
        var inputString = $('#inputID').val();
        var shortenedString = inputString.substr(0,(inputString.length -1));
    $('#inputID').val(shortenedString);
        console.log("Backspace button Pressed"+" "+inputString);

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="inputID" type="text" name="fname" class="myinput">
halfer
  • 19,824
  • 17
  • 99
  • 186
Hiren Jungi
  • 854
  • 8
  • 20