0

I have a checkbox.

If checked -> call one function displayBus(number)

If unchecked -> call other function displayCar()

The problem with this code is that I have to click also in hide or show to call the function.

  <input type="checkbox" id="mycheckbox" name="mycheckbox" ng-model="mycheckbox" />
        <div ng-show="mycheckbox">
             <a style="cursor: pointer;" ng-click="displayBus(number);">hide</a>

        </div>
        <div  ng-hide="mycheckbox">
            <a style="cursor: pointer;" ng-click="displayCar();">show</a>
        </div>

But I just want to check / Uncheck and then call automaticly the function. I DO NOT want to click on hide or show.

Thanks for your help.

1 Answer 1

7

You can wire up an ng-change to the checkbox and then check it's value:

<input type="checkbox" id="mycheckbox" name="mycheckbox" ng-model="mycheckbox" data-ng-change="dataChanged()" />

and then in your javascript:

$scope.dataChanged = function(){
    if($scope.mycheckbox){
        //checked
    }
    else{
        //not checked
    }
};

Edit:

If you want to do it only in html:

<a style="cursor: pointer;" ng-click="myCheckbox ? displayBus(number) : displayCar()">hide</a> 

Edit:

<input type="checkbox" id="mycheckbox" name="mycheckbox" ng-model="mycheckbox" data-ng-change="myCheckbox ? displayBus(number) : displayCar()" />
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but better if it is not in JavaScript but HTML
I need to have check/uncheck. Then it will execute displayBus(number) or displayCar()
Please describe your question a little better as I am confused on what you're trying to accomplish.

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.