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>