Home > Mobile >  forloop div event listeners not adding/removing classes
forloop div event listeners not adding/removing classes

Time:03-20

if (div.id === 'firstDiv')
            {
                div.addEventListener('click', function() {
                        div.classList.remove('w-1/4')
                        div.classList.add('w-full')
                })
            }

I have this code block in a for loop, it checks the id of each div in the loop, and adds an event listener to it. However, while the event listener works if I console.log it, it doesn't actually do anything with the classList

CodePudding user response:

The id of div is unique to every div so it just work in one div and you didn't defined which div should be change in the classlist so specify it with "this" keyword

  • Related