Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery removeClass() but not certain classes

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.

like image 365
Pinkie Avatar asked Oct 19 '11 18:10

Pinkie


People also ask

How do I remove a class from a query?

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.

How do you check if an element contains a class in jQuery?

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".

How do you remove an active class?

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.


1 Answers

Why don't you just replace the class attribute with the classes that you want like so

$('#yourElement').attr('class',"aa dd");
like image 87
Laurence Burke Avatar answered Sep 19 '22 18:09

Laurence Burke