DEV Community

mhsohag11
mhsohag11

Posted on

Answer: toggle class on click with javascript

You should use querySelectorAll instead of querySelector, then loop over all list items:

var listItems = document.querySelectorAll('li');
for(var i = 0; i < listItems.length; i++){
    listItems[i].addEventListener('click', function(event) {
      this.classList.toggle('clicked');
    });
}

Top comments (0)