Is there any better way to rewrite this:
$('element').removeClass('class1').removeClass('class2');
I cannot use removeClass();
as it would remove ALL classes, which I don't want.
Approach: First select the element to which multiple classes will be added. Then use addClass() method to add multiple classes to the element and removeClass() method to remove multiple classes.
jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.
The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
addClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements.
$("element").removeClass("class1 class2");
From removeClass()
, the class parameter:
One or more CSS classes to remove from the elements, these are separated by spaces.
$('element').removeClass('class1 class2');
Here are the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With