I want to remove a css class before adding a new class to an element. If I know which exact class the element has already, I can remove that by
$('#test').removeClass('current_class').addClass('new_class');
But the problem is that #test
does not have always same class. So I would like to remove any css class attached attached to #test
before adding before adding a new one. How should I do that?
Thanks.
$('#test').removeClass();
Will remove any class that the item has you can simply add your new class after that like you are doing now
$('#test').attr('class', '').addClass('new_class');
or
$('#test').removeClass().addClass('new_class');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With