0

I need help removing an EventListener on an anonymous function. I tried using an event handler but I also need to pass a element through the function to bind it.

// adds eventlistener to element
cardElement.addEventListener("click", flipCard.bind(this, cardElement));

// suppose to remove eventlistener
document.getElementById(cardsInPlay[0].id).removeEventListener("click", flipCard);
David Ko
  • 304
  • 1
  • 3
  • 11

2 Answers2

3

bind creates a new function, as you can read from the documentation

So what you can do is something like:

flipCard = flipCard.bind(this, cardElement);
cardElement.addEventListener("click", flipCard);
document.getElementById(cardsInPlay[0].id).removeEventListener("click", flipCard);
quirimmo
  • 9,800
  • 3
  • 30
  • 45
0

I ended up using a different method to remove the event listener. I deep cloned the node and replaced it.

var matchOne = cardOne.cloneNode(true);
cardOne.parentNode.replaceChild(matchOne, cardOne);
David Ko
  • 304
  • 1
  • 3
  • 11