I'm creating a game played by two people where an active player changes on a button click. The 1st code only changed the activeplayer once in two clicks. When I rewrote the code into the 2nd one, the problem got solved.
1st
document.querySelector('.switchPlayer').addEventListener('click', function(){
//switch current player
currentPlayer === 1 ? currentPlayer = 2 : currentPlayer = 1;
};
2nd
document.querySelector('.switchPlayer').onclick = function(){
//switch current player
currentPlayer === 1 ? currentPlayer = 2 : currentPlayer = 1;
};
After googling, the only thing I could understand was that addEventListener can have multiple handlers as opposed to onclick which can have only one handler.
If anybody could teach me why the 2nd code works, I would appreciate it.