jQuery Get and Set CSS Classes | jquery set class
ToggleclassWithjQuery,itiseasytomanipulatethestyleofelements.jQueryManipulatingCSSjQueryhasseveralmethodsforCSSmanipulation.Wewilllookatthefollowingmethods:addClass()-AddsoneormoreclassestotheselectedelementsremoveClass()-RemovesoneormoreclassesfromtheselectedelementstoggleClass()-Togglesbetweenadding/removingclassesfromtheselectedelementscss()-SetsorreturnsthestyleattributeExampleStylesheetThefollowingstylesheetwillbeusedforalltheexamplesonthispage:.important{ font-weight:bold; font-size:xx...
Toggle classWith jQuery, it is easy to manipulate the style of elements.
jQuery Manipulating CSSjQuery has several methods for CSS manipulation. We will look at the following methods:
addClass() - Adds one or more classes to the selected elements removeClass() - Removes one or more classes from the selected elements toggleClass() - Toggles between adding/removing classes from the selected elements css() - Sets or returns the style attribute Example StylesheetThe following stylesheet will be used for all the examples on this page:
.important { font-weight: bold; font-size: xx-large; }.blue { color: blue;}
jQuery addClass() MethodThe following example shows how to add class attributes to different elements. Of course you can select multiple elements, when adding classes:
Example$("button").click(function(){ $("h1, h2, p").addClass("blue"); $("div").addClass("important");});
Try it Yourself »[1]...