Morning,
Is there a wildcard i can use in jQuery which would remove a class from an element?
I am applying new classes to my info message box, if a users click it once, then clicks it again it is adding the new class, but retains the existing class.
I need to remove the previous class, then add the new class for styling. Any ideas?
<div id="infoMsg" style="display: block; " class="er-green er-red">All submitted feeds are complete.</div>
As you can see the original class was er-green, it has then added er-red.
jQuery removeClass() MethodThe 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.
removeClass() method: This method removes a class in the selector element in jQuery.
jQuery Manipulating CSSaddClass() - 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.
To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method.
Yip:
$(this).removeClass('er-green');
$(this).addClass('er-red');
Or remove them all first:
If a class name is included as a parameter, then only that class will be removed from the set of matched elements. If no class names are specified in the parameter, all classes will be removed.
$(this).removeClass();
$(this).addClass('er-red');
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