Skip to content

How to trigger backspace event in JavaScript | Example code

  • by

Use onkeydown event to check backspace key trigger or not in JavaScript.

Trigger backspace event in JavaScript example

HTML example code has an input field, if the user pressed a backspace key it will popup an alert box.

The backspace key char is 8, so we will check the condition with the same value.

<!DOCTYPE html>


  <input type="text" id="myInput">


    var input = document.getElementById('myInput');
    input.onkeydown = function() {
      var key = event.keyCode || event.charCode;
      if( key == 8 ){
        alert(event.code + " " + key);



Do comment if you have any doubts or suggestions on this JS event code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.