Search code examples
jquerytriggerskeyeventbackspace

Trigger backspace key in jQuery


How can I trigger the backspace key event in jQuery?

The following example isn't working:

var e = jQuery.Event("backspace", { keyCode: 8 });
$("#myarea").trigger( e );

Solution

  • You can't actually trigger it.

    You could, for example, remove the last character from a certain input, but you can't trigger the actual key.

    Example:

    var str = $('#input').val();
    $('#input').val(str.substring(0, str.length - 1));