Search code examples
javascriptjqueryback-buttonkeycode

Keycode for "Delete" triggers browser's back button — jQuery


I'm building a virtual keyboard that assigns images to keycodes, and appends them in spans after a keydown event. Problem comes with the DELETE functionality.

if (e.keyCode == 8) {
    $('span:last').remove(); 
}

Since everything happens outside of a textarea or input field, this triggers the browser's back button. Any help would be much appreciated!


Solution

  • That's a backspace, not a delete, for starters.

    Next, backspace is the keyboard shortcut for "Back", so you MUST return false; in the event handler to prevent that default action taking place.