Skip to content

Instantly share code, notes, and snippets.

@cstephe
Last active March 9, 2021 02:15
Show Gist options
  • Save cstephe/1ef2c48b136589935853 to your computer and use it in GitHub Desktop.
Save cstephe/1ef2c48b136589935853 to your computer and use it in GitHub Desktop.
Angular directive: prevent backspace from acting like the back button
.directive('backSpaceNotBackButton', [function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
// This will stop backspace from acting like the back button
$(element).keydown(function (e) {
var elid = $(document.activeElement)
.filter(
"input:not([type], [readonly]),"+
"input[type=text]:not([readonly]), " +
"input[type=password]:not([readonly]), " +
"input[type=search]:not([readonly]), " +
"input[type=number]:not([readonly]), " +
"input[type=email]:not([readonly]), " +
"input[type=date]:not([readonly]), " +
"input[type=datetime]:not([readonly]), " +
"input[type=datetime-local]:not([readonly]), " +
"input[type=month]:not([readonly]), " +
"input[type=tel]:not([readonly]), " +
"input[type=time]:not([readonly]), " +
"input[type=url]:not([readonly]), " +
"input[type=week]:not([readonly]), " +
"textarea")[0];
if (e.keyCode === 8 && !elid) {
return false;
}
});
}
}
}])
@khurshid007
Copy link

could you please explain how to use. i am new in angulrjs

thanks in advance
AKD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment