2

How can i emulate the backspace key function when i press the up arrow?

This is what i have so far and its not working :

if ( e.keycode === 38 ) {
        e.preventDefault();
        $(e.target).trigger({
            type: "keypress",
            which: 8
        };
EnigmaMaster
  • 213
  • 3
  • 11

3 Answers3

3

You can't trigger key presses in Javascript/JQuery for security reasons. Imagine if a website could take over your keyboard? Not a nice idea! :)

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
3

Description

You can use jQuery's keyup method to get this done. As far as i understand you want to remove the last char in the field. Checkout my sample and this jsFiddle Demonstration

Sample

$("input").keyup(function(e) {
     if (e.which=== 38 ) {
        e.preventDefault();
        var value = $("input").val();
        var newValue = $("input").val().substring(0, value.length-1);
        $("input").val(newValue);
     }
});
​

More Information

dknaack
  • 60,192
  • 27
  • 155
  • 202
2

IDEA:

maybe you could find the current position on a textarea or something and when the upkey is presed emulate the backspace key functon

Here says how to get the current position on a text input Caret position in textarea, in characters from the start

Community
  • 1
  • 1
chepe263
  • 2,774
  • 22
  • 38