3

I want to disable the up and down keys for a number input through angularjs directive (or otherwise).
Note: I don't want to change the input type to text as I have been asked not to. I already removed the spinners too. I am new to angular so don't know much about it. I googled through all the links I could find but they were mostly about hiding the spinners and not about disabling the arrow keys. I got this code from some other link. This disables the mousewheel for the same. If some changes to this can be done, that will be great.

module.directive('input',function() {
return {
    restrict: 'E',
    scope: {
        type: '@'
    },
    link : function (scope, $element) {
        if (scope.type == 'number') {
            $element.on('focus', function () {
                angular.element(this).on('keyup', function (e) {
                    e.preventDefault();
                });
            });
            $element.on('blur', function () {

                angular.element(this).off('keyup');
            });
        }
    }
}
});
6
  • Please move your question to the body of post. Please also post what you have tried and/or studied. Commented Nov 2, 2014 at 4:32
  • And what should I mention in the question title then? Commented Nov 2, 2014 at 4:41
  • Perhaps "Disable the up and down keys for a number input using angularjs". In the body of your question, you can then be as descriptive as necessary to get the information over to a potential responder. Commented Nov 2, 2014 at 4:43
  • I've rarely seen questions with the body visually shorter than the title. You should consider giving examples or wait for people to vote down or close your question Commented Nov 2, 2014 at 4:45
  • I have made some changes. Please let me know if I need to make more. Commented Nov 2, 2014 at 4:47

2 Answers 2

5

If you want to disable up/down arrow keys globally for all input[number] elements:

$(document).bind('keydown', function(event) {
  var target = event.srcElement || event.target;
  var tag = target.tagName.toUpperCase();
  var type = target.type.toUpperCase();
  if (tag === 'INPUT' && type === 'NUMBER' && 
     (event.keyCode === 38 || event.keyCode === 40)) {
    event.preventDefault();
  }
});

Or you can bind it to a single element with a directive:

.directive('disableArrows', function() {

  function disableArrows(event) {
    if (event.keyCode === 38 || event.keyCode === 40) {
      event.preventDefault();
    }
  }

  return {
    link: function(scope, element, attrs) {
      element.on('keydown', disableArrows);
    }
  };  
});

template:

 <input type="number" disable-arrows />
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. This works globally. But I want to restrict it to only some inputs. Can that be done?
1

You can change your directive to:

module.directive('input',function() {
return {
    restrict: 'E',
    scope: {
        type: '@'
    },
    link : function (scope, $element) {
        if (scope.type == 'number') {
            $element.on('focus', function () {
                angular.element(this).on('keyup', function (e) {
                    if (event.keyCode === 38 || event.keyCode === 40) {
                       e.preventDefault();
                    }
                });
            });
            $element.on('blur', function () {

                angular.element(this).off('keyup');
            });
        }
    }
}
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.