Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove class using jQuery

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.

like image 806
thatuxguy Avatar asked Jul 12 '12 11:07

thatuxguy


People also ask

Can you remove a class with jQuery?

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.

Which method is used to remove the class using jQuery?

removeClass() method: This method removes a class in the selector element in jQuery.

How add or remove a class 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.

How do you remove a class in CSS?

To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method.


1 Answers

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');
like image 166
ericosg Avatar answered Oct 27 '22 00:10

ericosg