How to swap CSS class names without touching the CSS codes



This technique might be very useful for this that deal with templates where the CSS themes may be overridden but you wish to keep certain classes references even when the template undergoes versioning.


 You can set the class (regardless of what it was) by using .attr(), like this:


If you want to add a class, use .addclass() instead, like this:


Or a short way to swap classes using .toggleClass():

$("#td_id").toggleClass('change_me newClass');

Here’s the full list of jQuery methods specifically for the class attribute.



Apply to entire document when you do not have an ID selector


$(document).ready(function(){ $('.blue').removeClass('blue').addClass('green');});

Leave a Reply