1

let firstplayerturn = 1;

if (firstplayerturn == 1) {
  value_input = document.getElementById("b1").value;
  if (value_input === "0") {
    alert("ERROR");
  }
  firstplayerturn = 0;
} else {

}
<input type="text" id="b1" class="box">

I was expecting that when first player is playing the game, "0" should not be taken as input. That's why I try:

if (value_input === "0") {
    alert("ERROR");
  }

1 Answer 1

1

Firstly, you don't have a function that executes your script when the input is entered.

let firstplayerturn = 1;

function check(){
if (firstplayerturn == 1) {
  value_input = document.getElementById("b1").value;
  if (value_input === "0") {
    alert("ERROR");
  }
  firstplayerturn = 0;
}
}
<input type="text" id="b1" class="box">
<button onclick="check()">Submit</button>

5
  • Instead of adding a button you could use the onclick event on the input tag itself. Dec 14, 2022 at 10:16
  • That won’t work because input can’t be clicked Dec 14, 2022 at 13:09
  • instead of onclick, you can use on of the following perhaps - onkeypress, onkeyup, onkeydown, onchange. I just found out, you can also use oninput: stackoverflow.com/questions/42237736/… Dec 14, 2022 at 18:04
  • Those will trigger it at the wrong time Dec 15, 2022 at 10:57
  • I’ve just now tried them, but they constantly execute multiple times when I type a big number. I doubt OP wanted to only have one digit numbers, nor have their script executed multiple times. Thus my answer. Dec 18, 2022 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.