0

I don't know to explain this, but let me try. I'm currently doing a snooker scorekeeper project, and I'll explain a problem that I have.

//Add point when player pots balls
buttons.yellowButton.addEventListener('click', () => {
  coloredBall(2); // add 2 points to player
})
buttons.greenButton.addEventListener('click', () => {
    coloredBall(3); // add 3 points to player
  })
  .
  .
  .
  .
  .
buttons.blackButton.addEventListener('click', () => {
  coloredBall(7); // add 7 points to player
})

The code above works fine, It just updates the player score. Now, when all the reds are potted, I want to disable all of the buttons except the button that the players suppose to play. So I create a function that will add a new event to the buttons. The thing is that when I restart a game, I want to be able to remove those events, and to adds the same event when all the reds are potted again. How can I do this?

allRedArePotted = function() => {
  buttons.yellowButton.disabled = false;
  buttons.yellowButton.addEventListener('click', () => {
    disableAllButtons();
    buttons.greenButton.disabled = false;
    yellow = 0;
  })
  buttons.greenButton.addEventListener('click', () => {
    disableAllButtons();
    buttons.brownButton.disabled = false;
    green = 0;
  })
  buttons.brownButton.addEventListener('click', () => {
    disableAllButtons();
    buttons.blueButton.disabled = false;
    brown = 0;
  })
  buttons.blueButton.addEventListener('click', () => {
    disableAllButtons();
    buttons.pinkButton.disabled = false;
    blue = 0;
  })
  buttons.pinkButton.addEventListener('click', () => {
    disableAllButtons();
    buttons.blackButton.disabled = false;
    pink = 0;
  })
  buttons.blackButton.addEventListener('click', () => {
    black = 0;
    checkWinner();
  })
}
Saeed
  • 5,413
  • 3
  • 26
  • 40
WYZku
  • 3
  • 3
  • 1
    Why you make snippets, please include relevant HTML in a [mcve] – mplungjan Sep 15 '21 at 10:24
  • I suggest you delegate and do NOT add event handlers on each element. You can then use dasta-attributes and localStorage to keep state – mplungjan Sep 15 '21 at 10:25
  • You're looking for [removeEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) however this is a bad approach. It's much better to keep the state of the snooker table in a variable and have the event listeners act accordingly. Like `if (allRedArePotted) dostuff();` where you simply set the variable to true at some point. –  Sep 15 '21 at 10:25
  • 1
    Duplicate: [JavaScript: remove event listener](https://stackoverflow.com/questions/4402287/javascript-remove-event-listener) –  Sep 15 '21 at 10:45
  • @mplungjan sorry man I'm new to this. I thought it would be easy to understand if I just post it in this way. Anyway, Thank you very much! – WYZku Sep 15 '21 at 14:57

1 Answers1

0

Use named functions (not anonymous) for event handlers and remove them anytime you want. For example:

// adding
document.addEventListener('click', clickHandler);

// removing
document.removeEventListener('click', clickHandler);

As mentioned in the comments, It is better to keep the state of your program somewhere and act according to that. Adding and Removing handlers is not a good approach.

someHandler(e) {
  if(condition) {
    // act1
  }
  else {
    //act2
  }
}
Saeed
  • 5,413
  • 3
  • 26
  • 40
  • It is better practice to write event listeners that do different stuff based on the app state. An answer should mention best practices, especially when recommending bad practice. –  Sep 15 '21 at 10:28
  • 1
    I think answers should contain both, the thing user looking for and after that they can introduce best practices. Thanks @ChrisG – Saeed Sep 15 '21 at 10:30
  • 1
    `Use named functions (not anonymous)` also anonymous ones can be used as long as they can somehow be referenced. So the `clickHandler` could be `function clickHandler() {}`, `const clickHandler = function() {}`, `const clickHandler = () => {}`, … – t.niese Sep 15 '21 at 10:38
  • 1
    Yes, I should have said "an answer should *also* mention best practices". However this is a dupe anyway: https://stackoverflow.com/questions/4402287/javascript-remove-event-listener –  Sep 15 '21 at 10:45