jqueryHow do I use the jQuery toggleClass() method?
The jQuery toggleClass() method is used to toggle between adding and removing one or more classes from the selected elements.
Example
$("button").click(function(){
$("div").toggleClass("active");
});
This example will toggle the active class on the div element when the button is clicked.
Explanation
The toggleClass() method takes two parameters:
className: A string representing one or more class names to be toggled.switch: A Boolean value that determines whether the class should be added or removed.
If the switch parameter is omitted, the class will be toggled on or off depending on the current state of the element.
Relevant Links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How can I use JQuery with Yii2?
- How can I prevent jQuery XSS vulnerabilities?
- How can I use jQuery to make an asynchronous HTTP (XHR) request?
- How do I use jQuery to zip files?
- How can I get the y position of an element using jQuery?
- How can I format a number using jQuery?
- How do I download a zip file using jQuery?
- How do I use jQuery to change the z-index of an element?
- How do I use jQuery to zoom in or out on an element?
See more codes...