How to change css styles of elements using jQuery

Sometimes when you work with jQuery,  you want to change css-style of elements. There are two common ways to do it: 1 – to change class of element, 2 – add css attribute

1. How to add class to element using jQuery

For example, you have a class 'green'

.green {
    background: #4cff00;

And you want to apply this class to your element. So you need to add class:


Note, that element with class '.ms-formtable' will have two classes - '.ms-formtable' and 'green'. If after some condition or action you want to return to defaults, then user removeClass:



2. How to add style to element using jQuery

If you can’t create a new class for a page, you can add a css-property for an object. The syntax is like this:

$('.ms-formtable').css('background', '#4cff00');

In this case you apply a new property to the object. To undo it you should use something like this:

$('.ms-formtable').css('background', 'none');


No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment Notes of web-specialist
Since 2009