mhyder1
mhyder1

Reputation: 107

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

Upvotes: 5

Views: 3935

Answers (2)

Girdhar Singh Rathore
Girdhar Singh Rathore

Reputation: 5585

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>

Upvotes: 4

Sudharsan S
Sudharsan S

Reputation: 15393

Instead of ng-change Use ng-click like this

<div ng-app="myApp">
    <div ng-controller="Ctrl">
        <label>Checkbox:
            <input type="checkbox" id="check1" ng-model="active" ng-click="check('check1')">
        </label>
    </div>
</div>

Fiddle

Upvotes: 0

Related Questions