How to add or remove CSS classes from an element with jQuery?

To add or remove CSS classes from an element using jQuery, you can use the addClass() and removeClass() methods, respectively.

To add a CSS class to an element, you can use the addClass() method:

// Select the element you want to add a CSS class to var element = $('.your-element'); // Add the CSS class element.addClass('your-class');

In the code above, replace your-element with the appropriate selector for the element you want to modify, and your-class with the name of the CSS class you want to add.

To remove a CSS class from an element, you can use the removeClass() method:

// Select the element you want to remove a CSS class from var element = $('.your-element'); // Remove the CSS class element.removeClass('your-class');

In the code above, replace your-element with the appropriate selector for the element you want to modify, and your-class with the name of the CSS class you want to remove.

Note that both the addClass() and removeClass() methods can be used to add or remove multiple classes at once by passing a space-separated list of class names as the argument. For example, element.addClass('class1 class2') will add both class1 and class2 to the element.