How do I disable backspace keystroke if anything other than 2 specific input fields are focused on using jquery?
here is my current code (NOW INCLUDING 2 TEXTBOXES):
I made a slight change to the accepted answer, which may be of use to someone:
$(document).keydown(function(e) {
var elid = $(document.activeElement).is("input, textarea") ;
if (e.keyCode === 8 && !elid) {
if(e.ctrlKey) {
window.history.back()
}
else {
alert("Navigating with Backspace has been disabled. Use CTRL + Backspace if you want to go back to the previous page (and lose any unsaved changes).");
return false;
}
}
});
With this method, the user can still navigate using Backspace by holding CTRL, but also it takes into account textarea as well as any input.
Of course, the alternative is to use something like this instead, which doesn't allow the user to leave the page if they've inputted anything.