Q:

jquery onclick toggle class next


$("button").click(function(){
  $("p").toggleClass("main");
 });
 
7
Definition and Usage
The toggleClass() method toggles between adding and removing one or more class names from the selected elements.

This method checks each element for the specified class names. The class names are added if missing, and removed if already set - This creates a toggle effect.

However, by using the "switch" parameter, you can specify to only remove, or only add a class name.

Syntax
$(selector).toggleClass(classname,function(index,currentclass),switch)

Example
Toggle between adding and removing the "main" class name for all <p> elements:
$("button").click(function(){
  $("p").toggleClass("main");
 });
 
1

New to Communities?

Join the community