javascript - html button won't call function -
trying call function called "clear()" in javascript won't work, function works when put console won't when click button
<script type="text/javascript" src="nandc.js" ></script> <span id="status">player1's turn (x)</span> <br> <button onclick="btnclick(11)" id="11"> </button> <button onclick="btnclick(12)" id="12"> </button> <button onclick="btnclick(13)" id="13"> </button> <br> <button onclick="btnclick(21)" id="21"> </button> <button onclick="btnclick(22)" id="22"> </button> <button onclick="btnclick(23)" id="23"> </button> <br> <button onclick="btnclick(31)" id="31"> </button> <button onclick="btnclick(32)" id="32"> </button> <button onclick="btnclick(33)" id="33"> </button> <br> <br> <button onclick="clear()" id="reset">reset</button>
i'm new html , javascript there obvious errors?
var playerturn=true; function btnclick(id) { if (document.getelementbyid(id).innerhtml!=" ") { return; } if (playerturn == true) { document.getelementbyid(id).innerhtml = "x"; statusmessage("player2's turn (o)"); } else { document.getelementbyid(id).innerhtml = "o"; statusmessage("player1's turn (x)"); } playerturn = !playerturn; } function statusmessage(text) { document.getelementbyid("status").innerhtml = text; } function clear() { playerturn=true; statusmessage("player1's turn (x)"); document.getelementbyid(11).innerhtml = " "; document.getelementbyid(12).innerhtml = " "; document.getelementbyid(13).innerhtml = " "; document.getelementbyid(21).innerhtml = " "; document.getelementbyid(22).innerhtml = " "; document.getelementbyid(23).innerhtml = " "; document.getelementbyid(31).innerhtml = " "; document.getelementbyid(32).innerhtml = " "; document.getelementbyid(33).innerhtml = " "; }
edit: added more code allow problem reproduced
its problem function name, try change clearme()
use link below, code works fine..
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick
..if change function name clear
not.
Comments
Post a Comment