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');
removeClass() Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
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.
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.
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');
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.
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