Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  179] [ 3]  / answers: 1 / hits: 16648  / 12 Years ago, thu, july 12, 2012, 12:00:00

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.


More From » jquery

 Answers
12

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');

[#84308] Wednesday, July 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;