How can i make so that X and 0 cant overwrite and only be pressed once on every space.
const inputs = document.querySelectorAll("input")
let clicks = 1;
for (let input of inputs) {
input.addEventListener('click', (evt) => {
input.value = ("0")
const id = evt.target.id;
const buttonNr = id[1];
if (clicks % 2 === 0) {
if(input.value = "X")
console.log(`Player X pressed ${buttonNr}`);
} else {
console.log(`Player 0 pressed ${buttonNr}`);
}
clicks++
})
}
I tried input.value = "" return; but that did not work unless i put it at the wrong place.
>Solution :
Try this:
const inputs = document.querySelectorAll("input")
let clicks = 1;
for (let input of inputs) {
input.addEventListener('click', (evt) => {
// Make sure input is empty
if(evt.target.value == "") {
// Increment "clicks"; check if (clicks modulo 2) == 0;
// if true place an "X" otherwise place an "O".
evt.target.value = (clicks++ % 2 == 0) ? "X" : "O";
}
})
}