Skip to content
Advertisement

can’t remove class after click again

I tried to color the text I clicked on by gave them a class and besides the text I click on the original color is back again, but when I click on the text twice the class can’t be removed. I use toggle class for this issue but doesn’t work.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

Cache the main list element, and the list items, and then attach one listener to the list element so you can use Event delegation to catch item click events as they “bubble up” the DOM.

When an item is clicked remove the red class from all the items and , depending on the conditional parameter you pass to toggle, (does the classList contain a red class) toggle the red class.

JavaScript
JavaScript
JavaScript

Additional documentation

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement