ADD AND REMOVE CSS CLASSES ON ELEMENTS WITH JQUERY

It is possible for you to dynamically add or remove CSS styles on elements through established class names. This is done by manipulating the class attribute on selected elements. Essentially, if you create an HTML tag such as <element>, you can dynamically convert it into <element class='classname'> and then back to <element>, all through jQuery. This process is similar to adding or removing attributes, as described earlier in this chapter, but different as a single class attribute can have multiple values.

There are four jQuery methods that you can use to manipulate an element’s class:

 $(selector).addClass(classname); $(selector).removeClass(classname); $(selector).hasClass(classname); if ...

Get HTML5: Your visual blueprint™ for designing rich web pages and applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.