Home/Jquery/.toggleClass() method in Jquery

.toggleClass() method in Jquery


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.



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
stateIt specity the boolean value for the class should be added or removed.
add: true ,  remove: false



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.



Leave a Reply