jeudi 29 juillet 2021

Click event and conditional statements

How do I create a click event in a group of links that checks if a link is already open and closes it while opening the new link in vanilla JavaScript

const heroList = document.querySelectorAll('.hero-list');
const navList = document.querySelectorAll('.nav-list');


for (let i = 0; i < heroList.length; i++) {
  
  heroList[i].addEventListener("click", function() {  
    navList[i].classList.toggle('show-list');
    heroList[i].addEventListener('click', function() {
      if(navList[i].classList.contains('show-list') == true) {
      navList[i].classList.remove('show-list')
     } else {
       navList[i].classList.add('show-list');
     }})
    
  })
 };

Aucun commentaire:

Enregistrer un commentaire