0

I need to select only two check boxes from the given multiple checkboxes and are not embedded in the list.

<input type="checkbox" class="two-checkbox" id="curr-EUR" ng-model="reqObject.EUR" ng-click="checkChanged()">EUR</input>
<input type="checkbox" class="two-checkbox" id="curr-JPY" ng-model="reqObject.JPY" ng-click="checkChanged()">JPY</input>
<input type="checkbox" class="two-checkbox" id="curr-INR" ng-model="reqObject.INR" ng-click="checkChanged()">INR</input>
<input type="checkbox" class="two-checkbox" id="curr-USD" ng-model="reqObject.USD" ng-click="checkChanged()">USD</input>

The above are my checkboxes and I need to select only two from them in angular way.

1

1 Answer 1

1

With HTML

<div ng-app="myApp">
    <div ng-controller="myCtrlr">
        <input type="checkbox" class="two-checkbox" id="curr-EUR" ng-model="reqObject.EUR" ng-click="checkChanged('EUR')">EUR</input>
        <input type="checkbox" class="two-checkbox" id="curr-JPY" ng-model="reqObject.JPY" ng-click="checkChanged('JPY')">JPY</input>
        <input type="checkbox" class="two-checkbox" id="curr-INR" ng-model="reqObject.INR" ng-click="checkChanged('INR')">INR</input>
        <input type="checkbox" class="two-checkbox" id="curr-USD" ng-model="reqObject.USD" ng-click="checkChanged('USD')">USD</input>
    </div>
</div>

the following script should do what you are looking for

angular.module('myApp', [])
    .controller('myCtrlr', function ($scope) {

        var arr = [undefined, undefined];

        $scope.reqObject = {
            EUR   : false,
            JPY   : false,
            INR   : false,
            USD   : false
        }

        $scope.checkChanged = function (v) {
            var key = arr.shift();
            if (key)
                $scope.reqObject[key] = false;

            arr.push(v);
            $scope.reqObject[v] = true;
        }
    })

angular.module('myApp', [])
  .controller('myCtrlr', function($scope) {

    var arr = [undefined, undefined];

    $scope.reqObject = {
      EUR: false,
      JPY: false,
      INR: false,
      USD: false
    }

    $scope.checkChanged = function(v) {
      var key = arr.shift();
      if (key)
        $scope.reqObject[key] = false;

      arr.push(v);
      $scope.reqObject[v] = true;
    }
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp">
        <div ng-controller="myCtrlr">
            <input type="checkbox" class="two-checkbox" id="curr-EUR" ng-model="reqObject.EUR" ng-click="checkChanged('EUR')">EUR</input>
            <input type="checkbox" class="two-checkbox" id="curr-JPY" ng-model="reqObject.JPY" ng-click="checkChanged('JPY')">JPY</input>
            <input type="checkbox" class="two-checkbox" id="curr-INR" ng-model="reqObject.INR" ng-click="checkChanged('INR')">INR</input>
            <input type="checkbox" class="two-checkbox" id="curr-USD" ng-model="reqObject.USD" ng-click="checkChanged('USD')">USD</input>
        </div>
    </div>

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.