How to filter element closures in JS?

  • 0
    Hello, how to make sure that when you click on the next menu, the previous one closes and the new one opens, I will be grateful for your help.
    JavaScript Myles Wheeler, Jun 2, 2020

  • 3 Answers
  • 0
    Somewhere something like this

    $dropDowns.forEach((el) => {
    el.addEventListener("click", () => {
    const menu = el.querySelector('.dropdown_menu')
    if (menu.classList.contains('active')) {
    menu.classList.remove("active")
    } else {
    menu.classList.add("active")
    $dropMenus.forEach((element) => {
    if (element === menu) {
    return
    } else {
    element.classList.remove("active")
    }
    });
    }
    });
    });
    Anonymous

  • 0
    Anonymous

  • 0
    $dropDowns.forEach((el) => {
    el.addEventListener("click", (e) => {
    $dropDowns.forEach( element => element.classList.remove("active") );
    e.target.classList.toggle("active");
    });
    });
    Adalyn Kramer

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!