Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - remove several classes

Tags:

jquery

Is there a more efficient way to remove several unneeded classes from dom elements at once?

JS

$('.one').removeClass('one');
$('.two').removeClass('two');
$('.three').removeClass('three');
like image 706
vuvu Avatar asked Nov 17 '14 12:11

vuvu


People also ask

Can removeClass remove multiple classes?

removeClass() Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

How can I delete one class and add another class in jQuery?

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.

Can we add multiple classes in jQuery?

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.


2 Answers

Selecting more then one element separated by comma, Removing more then one class from element separated by space

$('.one, .two, .three').removeClass('one two three');

DEMO

like image 96
Manwal Avatar answered Sep 19 '22 15:09

Manwal


If you want to remove multiple classes from one element, then separate classes by space.

http://api.jquery.com/removeClass/

One or more space-separated classes to be removed from the class attribute of each matched element.

If you want to remove different classes from different elements, I think then it will be better to make an array and remove classes by iterating over it.

like image 38
Syed Qarib Avatar answered Sep 19 '22 15:09

Syed Qarib