In the previous code, how can I in (JS) add & delete a class by clicking the button for example.
I searched a lot on the internet but couldn’t find anything I could understand
I think the easiest way to add, remove or toggle classes is using:
Generally, you should add an event listener to your button
addEventListener('click', function() {})
in its function you select the element where you want to change the classes
document.querySelector()
and then you use
classlist.add()
(or similar function) on the element to change its classes. In this article is a short example of a <span> that toggles its class when clicked: DOMTokenList: toggle() method - Web APIs | MDN
To give an specific solution you should tell us some more details:
Has every <li> its own button?
Is the button inside or outside the element it should change?
Should the classes on all <li> be add/removed together?
Is a button really needed or shall a click on the <li> element itself change its class?
I hope that gives a starting point. I’m happy to provide a more specific solution when I understand the details.