.addClass()
methodThe .addClass()
method in jQuery is used to add class name(s) to matched elements.
.addClass()
method Syntax.addClass('className') .addClass('className className') .addClass(function)
Parameter | Type | Description |
---|---|---|
className |
String | the class name to be added to the class-attribute of each matched element |
funtion(index, oldClassName) |
Function | a function returning one or more space-separated class names to be added. index is the position of the element in the set.(based 0) oldClassName is/are old class name(s). |
.addClass('className')
Add the class "selected" to the matched elements.
$("p").addClass("selected");
.addClass('className className')
Add the mulitple class "selected1","selected2" to the matched elements.
$("p").addClass("selected1 selected2");
.addClass(function)
adds the class "item-0" to the first
HTML
<ul> <li>Hello</li> <li>Hello</li> <li>Hello</li> </ul>
jQuery
$('ul li').addClass(function(index) { return "item-" + index; });