Description
The .addClass() method adds one or more classes to the selected elements.
This method does not remove existing class attributes, it only adds one or more values to the class attribute.
Tip: To add more than one class, separate the class names with space.
This method has the form:
.addClass(class)
Parameter | Description |
---|---|
class | (required) Specifies one or more class names to be added |
Return Value
This form returns a jQuery object.
Add Class Using a Function
Using a function to add classes to selected elements.
This method has the alternate form:
.addClass(function(index,oldclass))
Parameter | Description |
---|---|
function(index, oldclass) | (required) Specifies a function that returns one or more class names to be added.
|
Return Value
This form returns a jQuery object.
Examples
Add a class to the first p element:
$("button").click(function(){
$("p:first").addClass("intro");
});