jQuery toggleClass Method
jQuery toggleClass Method : is used to toggle class(es) to the selected elements. It Adds the class if it is not there on selected element and if it is there, it removes the class.
Syntax jQuery toggleClass Method
Here is syntax to create toggle class functionality-
$(selector).toggleClass(classname,function(index,currentClassName));
index: (Interger) Index of the current element in the current set.
toggleClassName(String)Current Class name in the matching set.
jQuery toggleClass() Method Example 1
<script type="text/javascript"> $(document).ready(function(){ $( "#toggleClassExample" ).on( "click", function(event) { $("#toggleClass").toggleClass('myclass'); }); }); </script> |
The above code will produce following result-
Note : This is screenshot of the output. To run this demo click on the above “Try it” button.
Advertisements