Toggle class with ng-click on several elements
Asked Answered
E

3

34

How can I toggle classes on several elements individually with ng-click?

In this question https://mcmap.net/q/451005/-angularjs-toggle-ng-class-ng-click toggling classes with a click was done like this:

CSS:

.red {
    color: red;
}

JS:

$scope.toggle = false;

HTML:

<button id="btn" ng-click="toggle = !toggle" ng-class="{'red' : toggle}">Change Class</button>

But what if I have several buttons that each should toggle its own class with ng-click?

If I set it up in this way:

HTML:

<button id="btn" ng-click="toggle = !toggle" ng-class="{'red' : toggle}">Change Class</button>
<button id="btn2" ng-click="toggle = !toggle" ng-class="{'red' : toggle}">Change Class</button>

Both buttons get toggled if I press one.

I know a workaround is to define an own ng-click event for each button (f.ex toggle1 for button1, toggle2 for button2) - but is this the best way?

Encephalomyelitis answered 5/8, 2014 at 14:20 Comment(0)
B
65

I made simple directive for testing:

module.directive('toggleClass', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('click', function() {
                element.toggleClass(attrs.toggleClass);
            });
        }
    };
});

so you can make any element toggle class you need

<button id="btn" toggle-class="active">Change Class</button>
<div toggle-class="whatever"></div>
Britannic answered 11/12, 2014 at 9:6 Comment(4)
How do you declare the two classes that will be toggled? i.e say you have a div: <div class="class_one">random content here</div>, and you want to toggle "class_one" to "class_two"? All of the examples I've seen for this just have active as the class being toggled.Cherilyncherilynn
@Jackson_Sandland in your case I think you want to use toggleClass twice, once for "class_one" and once for "class_two". All toggleClass does is check to see if the class is already there and act accordingly: if it is there, it removes it, and if it isn't there, it adds it.Fibrous
@zombi how can I toggle a class of an elements say "B", by clicking element "A"?Unclench
You can do it for example like this: .directive('toggleClass', function() { return { restrict: 'A', link: function(scope, element, attrs) { element.bind('click', function() { if (attrs.toggleClassOn) { var e = document.querySelectorAll(attrs.toggleClassOn); angular.element(e).toggleClass(attrs.toggleClass); } else { element.toggleClass(attrs.toggleClass); } }); } }; })Britannic
R
18

Depending on your requirements, you may be able to use an ng-repeat with an array representing the toggles. For example:

Your view:

<div ng-repeat="toggle in toggles">
    <button id="btn" ng-click="toggle.state = !toggle.state" ng-class="{'red' : toggle.state}">Change Class</button>
</div>

Inside your controller:

$scope.toggles = [{ state: true }, { state: false }, { state: true }];

This way you can expand on your button set by simply updating the array, or the internal array objects (should you need more complexity).

Radiotelegraphy answered 5/8, 2014 at 14:29 Comment(0)
W
1

inside controller

 $scope.stateToggle=false;
$scope.togglelasses=function(){
     $scope.stateToggle= !$scope.stateToggle;
}

in view use ng-class

 <button ng-click="togglelasses()" ng-class="stateToggle? 'active ': ' '">

add ng-class in every element where you want class active

Wooten answered 18/1, 2019 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.