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.

 

http://stackoverflow.com/questions/3452778/jquery-change-class-name

 

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

$("#td_id").attr('class','newClass');

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

$("#td_id").addClass('newClass');

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