How to add / remove a CSS class to/from an element
Thu, 12/20/2007 - 03:53 — sgrover
Adding and removing CSS classes is a great way to highlight changes to our pages. jQuery provides the .addClass() and .removeClass() methods.
$("#target").addClass("newclass");
$("#target").removeClass("newclass");
Both methods can take multiple class names, which will be added or removed as needed.
Another method can be used to acheive similar results - the .toggleClass() method. If the class does not already exist on the element,
it is added. Otherwise it is removed.
$("#target").toggleClass("newclass");
- Printer-friendly version
- Login or register to post comments
