Code completion result for source line: $(document).a| (QueryType=COMPLETION, prefixSearch=false, caseSensitive=true) ------------------------------------ METHOD addClass(className): jQuery [PUBLIC] jquery-api.xml
.addClass( className ) | Returns: jQuery |
className: One or more space-separated classes to be added to the class attribute of each matched element.
function(index, currentClass): 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, |
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.
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.