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.
- Using className Property.
- Using classList Property.
- item() Method.
- add() Method.
- contains() Method.
- remove() Method.
- toggle() Method.
- replace() Method.
- classList: This property returns the class name(s) of the element as a DOMTokenList object.
- contains(): This method returns a boolean value indicating if a particular class name is present or not.
- add(): This method is used to add one or more class names to an element.
How do I add toggle classes?
The jQuery toggleCLass() method is used to add or remove one or more classes from the selected elements. This method toggles between adding and removing one or more class name. It checks each element for the specified class names. If the class name is already set, it removes and if the class name is missing, it adds.
toggle() The toggle() method of the DOMTokenList interface removes a given token from the list and returns false . If token doesn’t exist it’s added and the function returns true .
How do you hide a button when clicked?