Created
August 25, 2014 20:01
-
-
Save mrnejc/2188beb5f997e0b4e866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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