How to manage css in jQuery?


How to manage css in jQuery?


Add new Class to an Element
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);


Remove Existing class from an element
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.
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()


Toggle Existing Class
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.


More on jQuery …

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

1 Trackback / Pingback

  1. jQuery Interview Questions - etechstuffs

Leave a Reply

Your email address will not be published.


5 × 4 =