2

I need to add a minus sign onclick of a button (-200), and on second click, the minus sign should be removed. And minus sign can be added only in the beginning.

<input type="button" value="-" name ="minus" id="minus" ng-click="click($event)" />

$scope.click = function(event){

    angular.element('.numValBox').focus();
    angular.element('.numValBox').val(function(index,val){
        return val + angular.element(event.target).val();			
    });

};

2
  • What's .numValBox? Commented Jul 13, 2015 at 9:05
  • Code is not clear . please provide fiddle Commented Jul 13, 2015 at 9:06

1 Answer 1

2

Here is a good approach to it:

JSFiddle

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
    <input type="button" value="-" name="minus" id="minus" ng-click="click($event)" />
    <input type="text" ng-model="numValBox" class="numValBox"></div>
</div>

JS:

angular.module('myApp', [])
.controller('myCtrl', function ($scope) {
    $scope.numValBox = "hello world";
    $scope.click = function (event) {
        $scope.numValBox = ($scope.numValBox.match(/-/)) ? $scope.numValBox.replace('-', '') : '-'+$scope.numValBox; 
    };
});

Of course I thought the input value should be anything not only numbers and that's because I did a match

Sign up to request clarification or add additional context in comments.

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.