How can I add or remove a class in Jquery?

To add or remove a class in jQuery, you can use the addClass() and removeClass() methods respectively.

Here’s an example of how to add a class to an element with the ID of “test-element“:

$('#test-element').addClass('add-class');

In the above example, we’re selecting the element with the ID of “test-element” using the $() function, and then calling the addClass() method on it, passing in the name of the class we want to add, which is “add-class“.

And here’s an example of how to remove a class from an element:

$('#test-element').removeClass('remove-class');

In this example, we’re selecting the element with the ID of “test-element” again, and then calling the removeClass() method on it, passing in the name of the class we want to remove, which is “remove-class“.

You can also use the toggleClass() method to toggle a class on or off an element, depending on whether it’s currently applied or not:

$('#test-element').toggleClass('selected');

In this example, we’re toggling the “selected” class on the element with the ID of “test-element“. If the class is already applied to the element, it will be removed; if it’s not applied, it will be added.