thatuxguy
thatuxguy

Reputation: 2528

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.

Upvotes: 13

Views: 19144

Answers (4)

Barlas Apaydin
Barlas Apaydin

Reputation: 7315

removeClass() method

$('#infoMsg').removeClass('er-green');//just removes class

$('div').click(function(){
   $(this).removeClass('er-green');//removes class clicked element
   $(this).addClass('er-red');//adds class clicked element
});

Upvotes: 6

Suave Nti
Suave Nti

Reputation: 3747

$("#infoMsg").removeClass('er-green').addClass('newclass') 

Upvotes: 5

Misam
Misam

Reputation: 4389

you can use jquery removeclass

$('#infoMsg').removeClass('er-green');

Like wise you can also add a css class to your html

$('#infoMsg').addClass('er-red');

Upvotes: 3

ericosg
ericosg

Reputation: 4965

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

Upvotes: 16

Related Questions