Skip to content

Instantly share code, notes, and snippets.

@mrnejc
Created August 25, 2014 20:01
Show Gist options
  • Save mrnejc/2188beb5f997e0b4e866 to your computer and use it in GitHub Desktop.
Save mrnejc/2188beb5f997e0b4e866 to your computer and use it in GitHub Desktop.
// via UX-SE
// http://ux.stackexchange.com/a/54443
var hasfocus = 'false';
// when focus happens, set a variable
$(document).on('focusin','input, textarea',function() {
hasfocus = 'true';
});
// unset when focus is not happening
$(document).on('focusout','input, textarea',function() {
hasfocus = 'false';
});
// if not in a form field, stop backspace and delete default action
$(document).keypress(function (e) {
if(e.which == 8 || e.which == 46) {
if(hasfocus == 'false') {
e.preventDefault();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment