addClass(class)
Adds the specified class name to each matched element.
Adds the specified class(es) to each of the set of matched elements.
Return value
jQuery
Parameters
class (String): One or more CSS class names to be added to the element, please separate them with spaces
Example
Add the 'selected' class to the matched elements
HTML code:
jQuery code:
$("p").addClass("selected");
Result:
Add selected highlight class to matched elements
HTML code:
jQuery code:
$("p").addClass("selected highlight");
Result:
-------------------------------------------------- -------------------------------------------------- --------------------------
removeClass(class)
Remove all or specified classes from all matching elements.
Removes all or the specified class(es) from the set of matched elements.
Return value
jQuery
Parameters
class (String): (optional) One or more CSS class names to be deleted, please separate them with spaces
Example
Remove 'selected' class from matched elements
HTML code:
jQuery code:
$("p").removeClass("selected");
Result:
Remove all classes of matching elements
HTML code:
jQuery code:
$("p").removeClass();
Result:
-------------------------------------------------- -------------------------------------------------- --------------------------
toggleClass(class)
Delete (add) a class if it exists (does not exist).
Adds the specified class if it is not present, removes the specified class if it is present.
Return value
jQuery
Parameters
class (String): CSS class name
Example
Toggle 'selected' class for matching elements
HTML code:
jQuery code:
$("p").toggleClass("selected");
Result: