I\'m trying to implement key-press functionality which will remove a div when the user hits Esc
. This works for Firefox & IE with the following code:
After the second alert add also
e.preventDefault();
This will prevent the default action of the event to be triggered.
More info about this method here
Your code should look like
$("body").keypress(function(e) {
alert("any key pressed");
if (e.keyCode == 27) {
alert("escape pressed");
e.preventDefault();
}});