Ways to capture the backspace and delete on the onkeydown event

23 Jan, 2020
Given the HTML document. The task is to detect when the backspace and delete keys are pressed on keydown events. Here 2 approaches are discussed, one uses event.key and another uses event.keyCode with the help of JavaScript.
Approach 1:
Take the input from input element and add a event listener to the input element using el.addEventListener() method on onkeydown event.
Use event.key inside the anonymous function called in the addeventlistener method to get the key pressed.
Check if the key pressed is Backspace or Delete.
Example 1: This example implements the above approach.
<!DOCTYPE HTML><html>  <head>    <title>        Capture the backspace and delete on the onkeydown event.    </title>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </script></head>  <body style="text-align:center;">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP">    </p>    Type Here:    <input id="inp" />    <br>    <p id="GFG_DOWN" style="color: green;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var el = document.getElementById('inp');        up.innerHTML = "Type in the input box to determine the pressed.";        el.addEventListener('keydown', function(event) {            const key = event.key;            if (key === "Backspace" || key === "Delete") {                $('#GFG_DOWN').html(key + ' is Pressed!');            }        });    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2:
Take the input from input element and add a event listener to the input element using el.addEventListener() method on onkeydown event.
Use event.keyCode inside the anonymous function called in the addeventlistener method to get the key pressed.
Check if the key’s code matches with the key code of Backspace or Delete button.
Example 2: This example implements the above approach.
<!DOCTYPE HTML><html>  <head>    <title>        Capture the backspace and delete on the onkeydown event.    </title>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </script></head>  <body style="text-align:center;">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP">    </p>    Type Here:    <input id="inp" />    <br>    <p id="GFG_DOWN" style="color: green;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var el = document.getElementById('inp');        up.innerHTML = "Type in the input box to determine the pressed.";        el.addEventListener('keydown', function(event) {            // Checking for Backspace.            if (event.keyCode == 8) {                $('#GFG_DOWN').html('Backspace is Pressed!');            }            // Checking for Delete.            if (event.keyCode == 46) {                $('#GFG_DOWN').html('Delete is Pressed!');            }        });    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript-Misc
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?