I want to confirm when someone unchecks a checkbox. The following function only works every other time. When it first loads its fine. It calls the confirm and if you cancel it leaves the checkbox checked. But if you click it again nothing happens. I'm sure it something simple that I'm missing:
$scope.check = function(clickedid) {
if (document.getElementById(clickedid).checked === true) {
return false;
} else {
var box= confirm("Are you sure you want to do this?");
if (box===true){ // yes sure
return true;
}
else{ // cancel
document.getElementById(clickedid).checked = true;
}
}
};
and here is the html
<lable>Checkbox
<input type="checkbox" id="check1" ng-model="active" ng-change="check('check1')">
</label>
Here is a jsfiddle
its because of ng-change
, It's working with ng-click
<lable>Checkbox
<input type="checkbox" id="check1" ng-model="active" ng-click="check('check1')">
</label>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With