Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs checkbox confirmation

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

like image 377
mhyder1 Avatar asked Sep 28 '22 04:09

mhyder1


1 Answers

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>
like image 119
Girdhar Singh Rathore Avatar answered Oct 04 '22 18:10

Girdhar Singh Rathore