Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a backspace on the onkeydown event

I have a function that is triggered by the onkeydown event of a textbox. How can I tell if the user has hit either the backspace key or the del key?

like image 756
Raphael Avatar asked Mar 01 '10 02:03

Raphael


People also ask

How do you get Backspace in JavaScript?

You can use addEventListener or onkeydown property to detect a used pressed backspace key or not in JavaScript. Where Backspace key keycode is 8 and key is “Backspace”.


1 Answers

Try this:

document.addEventListener("keydown", KeyCheck);  //or however you are calling your method function KeyCheck(event) {    var KeyID = event.keyCode;    switch(KeyID)    {       case 8:       alert("backspace");       break;        case 46:       alert("delete");       break;       default:       break;    } } 
like image 103
Stephano Avatar answered Oct 02 '22 17:10

Stephano