How to capture a backspace on the onkeydown event

Raphael picture Raphael · Mar 1, 2010 · Viewed 185.3k times · Source

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?

Answer

Stephano picture Stephano · Mar 1, 2010

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;
   }
}