Home > Blockchain >  how to prevent the innerHTML of change again after the click event
how to prevent the innerHTML of change again after the click event

Time:03-01

let player1 = document.getElementById("player1").innerHTML;
let player2 = document.getElementById("player2").innerHTML;

let turn = 0
let blocks = document.getElementsByClassName("blocks");

for (let i of blocks) {
  i.addEventListener("click", (event) => {
    i.innerHTML = turn
    if (turn == 0) {
      event.target.innerHTML = player1
      turn = 1
    } else {
      event.target.innerHTML = player2
      turn = 0
    }
  })
}
<div >

  <div>
    <div id="0" ></div>
    <div id="1" ></div>
    <div id="2" ></div>
  </div>

  <div>
    <div id="3" ></div>
    <div id="4" ></div>
    <div id="5" ></div>
  </div>

  <div>
    <div id="6" ></div>
    <div id="7" ></div>
    <div id="8" ></div>
  </div>

</div>

<div id="player1" >           
  • Related