Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
How to remove the event listeners of all buttons when only one is clicked

I'm doing a quiz app, and when the user picks an answer, I want to display the next question. The thing is, the event listener on the other buttons are not removed, only the button the user clicked (using once:true). Is there a way that I can remove all event listeners on the other buttons when only one of them is fired?

Here is my code:

   function enableButtons(correctAnswer) {
    
    btns.forEach((btn) => {
        btn.addEventListener(
            'click',
            function () {
                if (checkAnswer(btn.textContent, correctAnswer)) {
                    btn.classList.add('correctAnswer');
                } else {
                    btn.classList.add('wrongAnswer');
                }

                setTimeout(function () {
                    removeBtnClasses(btn),
                    nextQuestion()
                }, 2000);
            },
            {once: true}
        )
    })

}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can write removeEventListener method.

A guide for removeEventListener : https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener

And I am not sure about second method but I think you can add disabled attribute to the button, I haven't worked with DOM for long time so I don't remember if second method would work.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda