Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!