Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - disabling AND unchecking a checked box

I have a complex set of checkboxes in groups filled with data from multidimensional arrays and there are some specific things that need to happen with them... I extracted the minimum of what I need so I will try to explain

In this fiddle

http://jsfiddle.net/EVZUV/

the first checkbox is enabled and the second is disabled. When the first checkbox is checked the second becomes enabled. What I am trying to get is to UNCHECK the second box if it is becomes disabled again when the first checkbox is unchecked.

Here is the code I have that almost works

<input type="checkbox" ng-model="disablelist" ng-click="checkitems">Check this box to enable the other.. <br><br>
<input type="checkbox" ng-disabled="!disablelist" ng-checked="checkitems">When this checkbox is disabled it also must get unchecked

I am just getting intong-js and some ideas are pretty "whaaa .." to me,, but it looks good so I am persisting for now.

Thanks!!

like image 515
GRowing Avatar asked Nov 13 '13 22:11

GRowing


People also ask

How to disable checkbox in Angular JS?

We will use the Angular JS directive named ng-disabled to disable the button by unchecking the box. Please refer to AngularJS ng-disabled Directive. The ng-disabled directive is used to enable or disable the HTML elements.

How do I uncheck a checkbox in angular 6?

There are two approaches for this. Either modify your data and add a property that you'll bind to the checked property. Or use template variable for your checkboxes and access them in your . ts file.


2 Answers

Simple solution here: http://jsfiddle.net/7mKtF/2/

HTML:

<div ng-app="myApp">
    <input type="checkbox" ng-model="disablelist">Check this box to enable the other.. <br><br>
    <input type="checkbox" ng-disabled="!disablelist" ng-checked="disablelist && 0">When this checkbox is disabled it also must get unchecked
</div>

JS:

var app = angular.module('myApp', []);

Basically, I've updated your ng-checked statement to include the disablelist property. Using boolean logic, the second checkbox will evaluate to false if disablelist is false.

EDIT:

Also worth noting that the way you currently have it, your ng-click binding actually doesn't do anything. You would want to use ng-click to call a function variable, using argument parentheses.

like image 108
jedd.ahyoung Avatar answered Sep 17 '22 18:09

jedd.ahyoung


You can do it by assigning a model to your second checkbox, then reset it when you click on first checkbox.

HTML :

<input type="checkbox" ng-model="disablelist" ng-click="otherOption = false" /> Check this box to enable the other..
<input type="checkbox" ng-model="otherOption" ng-disabled="!disablelist" ng-checked="!!otherOption && !disableList" /> When this checkbox is disabled it also must get unckeched

Check the result in this jsFiddle

like image 38
meriadec Avatar answered Sep 19 '22 18:09

meriadec