Prevent backspace from navigating back in AngularJS

11,153

Solution 1

There is $document in angular js:

angular.module('yourModule', [])
  .controller('yourController', ['$scope', '$document', function($scope, $document) {
      $document.on('keydown', function(e){
          if(e.which === 8 && ( e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT" ) ){ // you can add others here inside brackets.
              e.preventDefault();
          }
      });

    }
  ]);

Plnkr Demo.

You can see in the demo i have used only for "INPUT" nodeName and it does not prevent the default of the backspace key on text input but not on textarea because we have not handled it in the condition.

Solution 2

I can't comment "accepted answer", but it will work not right, as condition

e.which === 8 && e.target.nodeName !== "INPUT" || e.target.nodeName !== "SELECT"

with logic error, so you can use

e.which === 8 && e.target.nodeName !== "INPUT" && e.target.nodeName !== "SELECT"

or answer that wrote @ThisIsMarkSantiago.

Solution 3

Add the below script in your controller

var rx = /INPUT|SELECT|TEXTAREA/i;

$document.on("keydown keypress", function(e){
    if( e.which == 8 ){ // 8 == backspace
        if(!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
            e.preventDefault();
        }
    }
});

Or you can use Jquery

  $(function(){
      var regx = /INPUT|SELECT|TEXTAREA/i;

      $(document).bind("keydown keypress", function(e){
          if( e.which == 8 ){ // 8 == backspace
              if(!regx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
                  e.preventDefault();
              }
          }
      });
  });
Share:
11,153
gyss
Author by

gyss

Web Software Engineer. Enjoying the JS fatigue

Updated on August 05, 2022

Comments

  • gyss
    gyss almost 2 years

    I faced this issue in my AngularJS webapp.

    When a user enters a page with a form to fill and he starts typing, if he presses the backspace key and the focus is not on the input text, then the page goes to the previous state.

    I looked up this solution using jQuery, but it doesn't seem the appropiate way for achieve this in AngularJS.

  • gyss
    gyss about 9 years
    It seems a pretty neat solution. But I've already got it working with jQuery. The idea is to solve this using AngularJS, if it's possible...
  • Jai
    Jai about 9 years
    angular internally uses jqLite. Nonetheless for this you just need to write javascript.
  • Jai
    Jai over 8 years
    @seb how come is that elementary error? frankly speaking this is just for backspace and anyone elem whether it is input or select.
  • Jai
    Jai over 8 years
    @seb just curious to know is there anything in a webpage where you can focus two elements at same time, because afaik not possible. So using && fails.
  • Seb
    Seb over 8 years
    See Tolya's answer. e.which === 8 && e.target.nodeName !== "INPUT" || e.target.nodeName !== "SELECT" is equivalent to (e.which === 8 && e.target.nodeName !== "INPUT") || e.target.nodeName !== "SELECT", so it is true whatever the key pressed if e.target.nodeName !== "SELECT.
  • Diego
    Diego about 8 years
    I can't backspace on password field, and I don't know the === operator. I fix it with: if ((nodeName == 'input' && (e.target.type == 'text' || e.target.type == 'password')) || nodeName == 'textarea') {
  • SajithK
    SajithK almost 8 years
    you should consider editable tags also in if condition. if (e.which === 8 && !e.target.isContentEditable && (!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly)) { e.preventDefault(); }