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

135
Views
Disable an addEventListener function when needed

This function is needed for some time. But when tasks are completed I need to disable it, that after clicking on 'board' it wouldn't work.

board.addEventListener('click', event => {
    if (event.target.classList.contains('circle')) {
        score++
        event.target.remove()
        createRandomCircle()
    } else if (!event.target.classList.contains('circle')) {
        fault++
    }
})

//Here I want to deactivate this event listener

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would call a handler function that returns a new named function (a closure) to act as the listener. You can then go through your tasks, and remove the listener when they're complete.

const board = document.querySelector('#board');

board.addEventListener('click', handler(), false);

function handler() {

  // This is the function the listener uses
  return function listener(e) {
    console.log(e.target);
    console.log('Task one');
    console.log('Task two');
    board.removeEventListener('click', listener);
    console.log('The button no longer works.');
  }

}
<button id="board">Board</button>

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!