toggle() method of the
classList property can be used to toggle a CSS class from an HTML element. This method takes in the class name as input, and toggle it.
Let us say you have got the following HTML element:
The following example demonstrates how you can add more
olive to the
pizza by using the
const div = document.querySelector('div'); div.classList.toggle('olive');
classList property works in all modern browsers, and IE10 and above.