Skip to content
Advertisement

Simple Question Vanilla JS Show Unmatch Buttons

I have 3 buttons. When I click one buttons in array of element, others will be disabled. How can i log unmatch buttons. Please help! thank you

  btnItem.addEventListener("click", () => {
    btnList.forEach((activeCurrent) => {
      if (btnItem.id === activeCurrent.id) {
        //Have Three Button Select active clicked console.log(show others)
      }
    });
  });
});

Advertisement

Answer

(function() {
  const buttonList = document.querySelectorAll('button')
  
  const onClickButton = (e) => {
    buttonList.forEach(button => {
      if (button !== e.currentTarget) {
        console.log(button)
      }
    })
  }
  
  buttonList.forEach(button => button.addEventListener('click', onClickButton))
}
)();
<button>btn1</button>
<button>btn2</button>
<button>btn3</button>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement