jQuery - CSS Classes Manipulation
In the beginning of this section we've learned how to change the CSS of elements by using the css() method.
$(".example").css("display", "none");
We also learned that the code can be simplified by using the hide() method.
Let's see a few useful methods jQuery offers to interact with HTML elements and their classes.
addClass(), removeClass(), toggleClass()
The css method works well when we just need to change one or two properties. But when we need to change multiple properties, it's better to work with classes. Let's change the object below completely by adding a class.
This paragraph has the class "normal". Let's add the "styling" class.
// add class:
$("#add_class").click(function(){
$("#example-paragraph").addClass("styling");
});
// remove class:
$("#remove_class").click(function(){
$("#example-paragraph").removeClass("styling");
});
// toggle class:
$("#toggle_class").click(function(){
$("#example-paragraph").toggleClass("styling");
});