0

I'm attempting to create a tic tac toe game in JavaScript. The problem is that I don't know how to know the position of the click that came from. This code works only for botton 1!

This is the JavaScript:

var turn = 0

function clicke() {
   console.log(mat)
   if(turn==0)
   {
      document.getElementById("1").innerHTML = "O";
      turn = turn + 1; 
   } else
   {
      document.getElementById("1").innerHTML = "X";
      turn = turn - 1; 
   }

}

this is the html where the X and O will be visualized:

<div>
    <div class="hbottoni">
        <button id="1"type="button" class="btn btn-danger" onclick="clicke()">1</button>
        <button type="button" class="btn btn-danger">2</button>
        <button type="button" class="btn btn-danger">3</button>
    </div>
    <div>
        <button type="button" class="btn btn-danger">4</button>
        <button type="button" class="btn btn-danger">5</button>
        <button type="button" class="btn btn-danger">6</button>
    </div>
    <div>
        <button type="button" class="btn btn-danger">7</button>
        <button type="button" class="btn btn-danger">8</button>
        <button type="button" class="btn btn-danger">9</button>
    </div>
</div>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Pasta24z
  • 19
  • 1
  • unfortunately we can only help you with your existing code, not code an entire solution for you. I think you should ask something specific so we could help you. Cheers – Orange Orange Feb 01 '20 at 14:03
  • A simple solution would be pass an extra argument. For example, `onclick="clicke(1)"`, and `onclick="clicke(2)"`, etc. and the function's signature would be `clicke(id) {...}`. For the whole example, you can check: https://tictactoe.js.org/ – Jeff Tian Feb 01 '20 at 14:11

0 Answers0