.addClass()
Adds the specified class(es) to each element in the set of matched elements.
.addClass(className)🡢 jQuery
className
| String | One or more space-separated classes to be added to the class attribute of each matched element. |
.addClass(classNames)🡢 jQuery
classNames
| Array | An array of classes to be added to the class attribute of each matched element. |
.addClass(function(indexInteger, currentClassNameString))🡢 String
function(indexInteger, currentClassNameString)
| Function | A function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, this refers to the current element in the set. |
.addClass(function(indexInteger, currentClassNameString))🡢 String, Array
function(indexInteger, currentClassNameString)
| Function | A function returning one or more space-separated class names or an array of class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, this refers to the current element in the set. |
It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.
Before jQuery version 1.12/2.2, the .addClass()
method manipulated the className
property of the selected elements, not the class
attribute. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).
As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class
attribute is used instead. So, .addClass()
can be used on XML or SVG documents.
More than one class may be added at a time, separated by a space, to the set of matched elements, like so:
$("p").addClass("myClass yourClass");
This method is often used with .removeClass()
to switch elements' classes from one to another, like so:
$("p").removeClass("myClass noClass").addClass("yourClass");
Here, the myClass
and noClass
classes are removed from all paragraphs, while yourClass
is added.
As of jQuery 1.4, the .addClass()
method's argument can receive a function.
$("ul li").addClass(function (index) {
return "item-" + index;
});
Given an unordered list with two <li>
elements, this example adds the class "item-0" to the first <li>
and "item-1" to the second.
Add the class "selected" to the matched elements.
<p>Hello</p>
<p>and</p>
<p>Goodbye</p>
p {
margin: 8px;
font-size: 16px;
}
.selected {
color: blue;
}
.highlight {
background: yellow;
}
$("p").last().addClass("selected");
Add the classes "selected" and "highlight" to the matched elements.
<p>Hello</p>
<p>and</p>
<p>Goodbye</p>
p {
margin: 8px;
font-size: 16px;
}
.selected {
color: red;
}
.highlight {
background: yellow;
}
$("p").last().addClass("selected highlight");
Add the classes "selected" and "highlight" to the matched elements (3.3+ syntax).
<p>Hello</p>
<p>and</p>
<p>Goodbye</p>
p {
margin: 8px;
font-size: 16px;
}
.selected {
color: red;
}
.highlight {
background: yellow;
}
$("p").last().addClass(["selected", "highlight"]);
Pass in a function to .addClass()
to add the "green" class to a div that already has a "red" class.
<div>This div should be white</div>
<div class="red">
This div will be green because it now has the "green" and "red" classes. It
would be red if the addClass function failed.
</div>
<div>This div should be white</div>
<p>There are zero green divs</p>
div {
background: white;
}
.red {
background: red;
}
.red.green {
background: green;
}
$("div").addClass(function (index, currentClass) {
var addedClass;
if (currentClass === "red") {
addedClass = "green";
$("p").text("There is one green div");
}
return addedClass;
});