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...
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
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');
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
});
$("#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');