Description:

The jQuery toggleCLass() method is used to add or remove one or more classes from the selected elements. ย In simple, the toggleClass() method checks each element for the specified class names. ย If the class name is already presented for the selected element then it will remove else it will add the class.

Syntax

$(selector).toggleClass(classname,function(index,currentclass),state)

classNameย  It specify One or more class names to add/remove from the selected set. More than one class name will be separated by the empty space
function(index,currentclass) A function that returns one or more class names to remove
index : Returns the index position of the matched element
currentclass : Returns the current class name of the matched element
state It specity the boolean value for the class should be added or removed.
add: true , ย remove: false

Example:ย 

Output:ย 

As per code, ย on clicking of “Toggle CSS Class” button, the toggleClass method will verifyย the selected div with the “CustomClass”, if its notย present then it will addย the class else it will remove the class as shown below.

dotnet-helpers-com-jquery-toggleclass-method-thiyagu-jquery-helpers