0

I have some checkbox, and a button. Now, I want active button when one or more checkbox is checked. How can I do this in angularjs?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<label><input type="checkbox" name="check"> First</label>
<label><input type="checkbox" name="check"> Second</label>
<label><input type="checkbox" name="check"> Third</label>


<br>
<button type="button" disabled ng-model="$scope.btn">active/deactive</button>

3
  • 2
    save the values of the checkboxes with ng-model and use them in ng-disabled="!(checkboxValue1 || ch...)" Commented Sep 6, 2016 at 8:32
  • 1
    you want to activate button if two boxes are checked?? Commented Sep 6, 2016 at 8:33
  • @SaEChowdary even if one of them is checked, one, two, three or ... Commented Sep 6, 2016 at 8:34

3 Answers 3

1

Set ng-model to checkboxes

<label><input type="checkbox" name="check" ng-model="check1"> First</label>
<label><input type="checkbox" name="check" ng-model="check2"> Second</label>
<label><input type="checkbox" name="check" ng-model="check3"> Third</label>

And use ng-disabled to disable\enable button

<button type="button" ng-disabled="!check1 && !check2 && !check3" ng-model="$scope.btn">active/deactive</button>
Sign up to request clarification or add additional context in comments.

2 Comments

suppose if i have n boxes??do i need to extend upto !checkn??isnt there any simplest way?
hello @Razzaka i want to also using checkbox set button disable/enable but my check box is it store in datatable angulrjs. so i m assign ng-modal='check' liek this so i m check on chekbox at time all check box checked so can you idea how can fix it.
1

You can use the ng-disabled directive on the button to add the disable attribute conditionally.

Check the below example where the input check boxes are bound to their respective properties on the controller's scope using the ng-model directive that serves two-way data binding in AngularJS.

Note: If you're having a complex condition to be checked in the view you can write a function and invoke it in the view using a corresponding directive in that situation.

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController);
  
  function DefaultController() {
    var vm = this;
    vm.disableButton = disableButton;
    
    function disableButton() {
      if (vm.first || vm.second || vm.third) {
        return false;
      } else {
      	return true;
      }
    }
  }
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController as ctrl">
    <label><input type="checkbox" name="check" ng-model="ctrl.first"> First</label>
    <label><input type="checkbox" name="check" ng-model="ctrl.second"> Second</label>
    <label><input type="checkbox" name="check" ng-model="ctrl.third"> Third</label>
    <br>
    <button type="button" ng-disabled="ctrl.disableButton()">active/deactive</button>
  </div>
</div>

1 Comment

Thanks for your answer
0
  1. save the values of the checkboxes with ng-model and use them in ng-disabled="!(checkboxValue1 || ch...)"

  2. use ng-model for saving the values and ng-requried="!checkBox2Value && !checkbox3Value" with the checkboxes. Put a form around the checkboxes and use ng-disabled="formName.$invalid"

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.