{"name":"addClass","type":"method","title":".addClass()","deprecated":null,"removed":null,"desc":"Adds the specified class(es) to each of the set of matched elements.","categories":["attributes","manipulation/class-attribute","css","version/1.0","version/1.4"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":{"desc":"One or more space-separated classes to be added to the class attribute of each matched element.","name":"className","type":"String"}},{"added":"1.4","argument":{"desc":"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.","name":"function(index, currentClass)","type":"Function"}}],"examples":[{"desc":"Add the class \"selected\" to the matched elements.","code":"\n$( \"p\" ).last().addClass( \"selected\" );\n","css":"\n p {\n margin: 8px;\n font-size: 16px;\n }\n .selected {\n color: blue;\n }\n .highlight {\n background: yellow;\n }\n","html":"\n

Hello

\n

and

\n

Goodbye

\n"},{"desc":"Add the classes \"selected\" and \"highlight\" to the matched elements.","code":"\n$( \"p:last\" ).addClass( \"selected highlight\" );\n","css":"\n p {\n margin: 8px;\n font-size: 16px;\n }\n .selected {\n color: red;\n }\n .highlight {\n background: yellow;\n }\n","html":"\n

Hello

\n

and

\n

Goodbye

\n"},{"desc":"Pass in a function to .addClass() to add the \"green\" class to a div that already has a \"red\" class.","code":"\n$( \"div\" ).addClass(function( index, currentClass ) {\n var addedClass;\n\n if ( currentClass === \"red\" ) {\n addedClass = \"green\";\n $( \"p\" ).text( \"There is one green div\" );\n }\n\n return addedClass;\n});\n","css":"\n div {\n background: white;\n }\n .red {\n background: red;\n }\n .red.green {\n background: green;\n }\n","html":"\n
This div should be white
\n
This div will be green because it now has the \"green\" and \"red\" classes.\n It would be red if the addClass function failed.
\n
This div should be white
\n

There are zero green divs

\n"}],"longdesc":"\n

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.

\n

More than one class may be added at a time, separated by a space, to the set of matched elements, like so:

\n
\n$( \"p\" ).addClass( \"myClass yourClass\" );\n    
\n

This method is often used with .removeClass() to switch elements' classes from one to another, like so:

\n
\n$( \"p\" ).removeClass( \"myClass noClass\" ).addClass( \"yourClass\" );\n    
\n

Here, the myClass and noClass classes are removed from all paragraphs, while yourClass is added.

\n

As of jQuery 1.4, the .addClass() method's argument can receive a function.

\n
\n$( \"ul li\" ).addClass(function( index ) {\n  return \"item-\" + index;\n});\n    
\n

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.

\n "}]}