jQuery Add and Remove CSS Class


To control the CSS class given to HTML elements, jQuery provides numerous methods such as addClass(), removeClass(), toggleClass(), and so on.


jQuery addClass() Method

The jQuery addClass() function adds one or more class to the components that have been chosen.

Example

Preview

You may also apply several class to components at the same time. Simply enter a space-separated list of class into the addClass() function, like this:

Example

Preview

jQuery removeClass() Method

Similarly, the jQuery removeClass() function may be used to remove class from items. The removeClass() function can be used to remove a single class, several class, or all class from the specified components at once.

Example

Preview

When the removeClass() function is used without an argument, it removes all class from the items that have been chosen. Here's an illustration:

Example

Preview

jQuery toggleClass() Method

The jQuery toggleClass() function adds or removes one or more class from the selected elements in such a way that if an element already has the class, it is deleted; if an element does not have the class, it is added (i.e. toggle class).

Example

Preview