dream genie
CMS and Website Builder
contact us
+44 (0)1392 202078
You are here: Home > Blog > Use JQuery to Add Remove CSS classes   Go Back

Use JQuery to Add Remove CSS classes

Posted: Friday, 10th December, 2010 at 7:42 AM | By: Administrator


Here's our scenario, we don't have access to the source HTML but we have some new cool predefined CSS class we want to add to exisitng element(s).

So for example there's an existing DIV to which we want to add some predefined styling to, but rather than use CSS to have to add the new styling would like to be able to just add the class that contains our styling.

This is much tidier and has the advantage that if we do this over more than 1 element then any change to the one pre-defined CSS class is immediatley reflected to all elements using it.

JQuery has two function to help us out, .addClass and .removeClass


It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.

More than one class may be added at a time, separated by a space, to the set of matched elements, like so:

$('p').addClass('myClass yourClass'); 


This method is often used with . removeClass to switch elements classes from one to another like so...

$('p').removeClass('myClass noClass').addClass('yourClass'); 

reference : JQuery addClass


Visit the blog archive »

You are welcome to post a comment

To Verify You are Human!
Please enter the code above:

Previous Comments

andrew wrote:
30 April 2015 10:39


andrew wrote:
30 April 2015 10:39