I searched for good cross-browser code that will disable backspace when in a drop-down element. Before you say that's bad design, don't disable shortcuts the user may want, know that it's a requirement that I cannot talk the business analyst out of. I first tried attaching the event to the drop-downs themselves, but was having issues with that. So now it runs for the entire document.
Here is my solution:
function noBackspace(evnt)
{
if (window.event) // IE
{
evnt = window.event;
if (window.event.keyCode == 8 && evnt.srcElement.type == "select-one")
{
evnt.cancelBubble = true;
evnt.keyCode = 0;
evnt.returnValue = false;
return false;
}
}
else if (evnt.which) // Firefox/Netscape/Opera
{
if (evnt.which == 8 && evnt.target.type == "select-one")
{
evnt.stopPropagation();
evnt.returnValue = false;
return false;
}
}
return true;
}
window.onload = function () { document.onkeydown = noBackspace; };
No comments:
Post a Comment