Advantage of styling with CSS classes in JavaScript

Using classes instead of changing styles directly has another benefit. With a slight movement of the hand, you can make the styles of the elements toggle.

For example, you can make it so that when you first click on a paragraph, it will be painted in a certain color, and when you click again, it will return to its original color. To do this, you just need to change the add method to the toggle method:

let elems = document.querySelectorAll('p'); for (let elem of elems) { elem.addEventListener('click', function() { this.classList.toggle('colored'); }); }

Modify the previous task so that pressing the button again cancels the action of this button.

enru