Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing multiple classes (jQuery)

Tags:

jquery

css

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.

like image 338
eozzy Avatar asked Sep 28 '09 06:09

eozzy


People also ask

How do you delete multiple classes?

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.

What does addClass do in jQuery?

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.

What is removeClass in jQuery?

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 add or remove a class in jQuery?

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.


2 Answers

$("element").removeClass("class1 class2"); 

From removeClass(), the class parameter:

One or more CSS classes to remove from the elements, these are separated by spaces.

like image 69
cletus Avatar answered Sep 21 '22 17:09

cletus


$('element').removeClass('class1 class2');

Here are the docs.

like image 30
Scott Evernden Avatar answered Sep 22 '22 17:09

Scott Evernden