Strictly removing and adding a class with the toggleClass method in jQuery
You can also pass a state parameter to toggleClass like true or false, then the class will either only be added or only removed.
Let's say we have the following CSS classes:
.red {
color: red;
}
.zzz {
font-style: italic;
}
The paragraph class zzz is applied:
<p class="zzz" id="test">text</p>
<button>click</button>
Let's just add the red class when clicked:
$('button').click(function() {
$('#test').toggleClass('red', true);
});
As you can see, pressing it again does not change anything.
All three listed methods for working with classes - addClass, removeClass, toggleClass can also take multiple classes as a parameter, and also allow applying a function to each element in the set.
Correct the code of the example given above - initially add the class red to the attribute and then make it so that the class red is only removed.