13

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.

4 Answers 4

16

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');
Sign up to request clarification or add additional context in comments.

Comments

6

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 Comment

moto, can it moto become something that it makes somo
5
$("#infoMsg").removeClass('er-green').addClass('newclass') 

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.