The code below is supposed to add an event listener for each playing card in a players hand while it's their turn, and then remove the events while it's a different player's turn.
It isn't working. That player's cards remain clickable once the event is initially set on their first turn.
takeTurn ( playerIndex ) {
console.log(this.name);
let handCards = document.getElementById(this.name).querySelector('.handCards');
let theCards = handCards.querySelectorAll('.handCards .card');
let that = this;
for ( let h = 0; h < theCards.length; h++ ) {
theCards[h].addEventListener("click", function onPlayCard (){
let theseCards = handCards.querySelectorAll('.handCards .card');
let discarded = that.playCard(theCards[h], theseCards, handCards);
that.gameInstance.discardPile.push(discarded);
console.log(that.gameInstance.discardPile);
for ( let e = 0; e < theseCards.length; e++) {
theseCards[e].removeEventListener("click", onPlayCard, false);
}
console.log(that.name + 'is done');
that.gameInstance.nextPlayer( playerIndex );
}, false);
}
}
I tried some of the ideas from here and here, but none of them quite solved the problem.
Any helps are appreciated. I might pull my hair out soon. I thought I knew this stuff.