Answer» Query provides several methods to manipulate the CSS classes assigned to HTML elements. The most important methods are addClass(), removeClass() and toggleClass().
addClass(): This method adds one or more classes to the selected elements.
- Syntax: $(selector).addClass(className);
- You can also add multiple classes to the selector. Syntax:$(selector).addClass(class1, class2);
removeClass(): Similar to adding class, you can also remove the classes from the elements by using this method.
- The removeClass() method can remove a single class, multiple classes, or all classes at once from the selected elements.
- Syntax:
- For removing one class: $(selector).removeClass(class1);
- For removing multiple class: $(selector).removeClass(class1, class2, class 3);
- For removing all classes at once: $(selector).removeClass()
toggleClass(): This method is used for adding or removing one or more classes from the selected elements in such a way that if the selected element already has the class, then it is removed. Else if an element does not have the specified class, then it is added i.e. it toggles the application of classes.- Syntax: $(selector).toggleClass(className);
|