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'
1 2 3 |
.green { background: #4cff00; } |
And you want to apply this class to your element. So you need to add class:
1 |
$('.ms-formtable').addClass('green'); |
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:
1 |
$('.ms-formtable').removeClass('green'); |
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:
1 |
$('.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:
1 |
$('.ms-formtable').css('background', 'none'); |