How can I do remove all classes from an element except certain classes. I'm assuming we can't use not with removeClass()
. Let say I have a <div class="aa bb cc dd ee ff"></div>
and I want to remove all classes except aa dd
. How can I do this.
jQuery removeClass() Method 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.
jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".
removeClass() Method. This method removes one or more class names from the selected elements. If no parameter is specified in the removeClass() method, it will remove all class names from the selected elements.
Why don't you just replace the class attribute with the classes that you want like so
$('#yourElement').attr('class',"aa dd");
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