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>All submitted feeds are complete.</div> 

As you can see the original class was er-green, it has then added er-red.

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

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 }); 
1
$("#infoMsg").removeClass('er-green').addClass('newclass') 

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy