jQuery addClass() Method | jquery set class
❮jQueryHTML/CSSMethods[1]ExampleAddaclassnametothefirst
element:$("button").click(function(){ $("p:first").addClass("intro");});TryitYourself»[2]DefinitionandUsageTheaddClass()methodaddsoneormoreclassnamestotheselectedelements.Thismethoddoesnotremoveexistingclassattributes,itonlyaddsoneormoreclassnamestotheclassattribute.Tip:Toaddmorethanoneclass,separatetheclassnameswithspaces.Syntax$(selector).addClass(classname,function(index,currentclass))ParameterDescriptionclassnameRequired.Sp...
❮ jQuery HTML/CSS Methods[1]
ExampleAdd a class name to the first <p> element:
$("button").click(function(){ $("p:first").addClass("intro");});
Try it Yourself »[2] Definition and UsageThe addClass() method adds one or more class names to the selected elements.
This method does not remove existing class attributes, it only adds one or more class names to the class attribute.
Tip: To add more than one class, separate the class names with spaces.
Syntax$(selector).addClass(classname,function(index,currentclass))
Parameter Description classname Required. Specifies one or more class names to be added function(index,currentclass) Optional. Specifies a function that returns one or more class names to be addedindex - Returns the index position of the element in the set currentclass - Returns the current class name of the selected elementTry it Yourself - Examples
Add two classes to...