0
I am learning Javascript and want to make an old game to train. The game is not working and I think it may be a logic error.
function verificar(elemento) {
var teste = document.getElementById(elemento).innerHTML;
if (teste == "") {
teste = "X";
} else {
teste = "Y";
}
}
.container {
max-width: 130px;
}
.tab {
float: left;
height: 30px;
width: 30px;
border: solid green 2px;
}
<div class="container">
<div class="tab" id="q1" onlick="verificar(this.id);"></div>
<div class="tab" id="q2" onlick="verificar(this.id);"></div>
<div class="tab" id="q3" onlick="verificar(this.id);"></div>
<div class="tab" id="q4" onlick="verificar(this.id);"></div>
<div class="tab" id="q5" onlick="verificar(this.id);"></div>
<div class="tab" id="q6" onlick="verificar(this.id);"></div>
<div class="tab" id="q7" onlick="verificar(this.id);"></div>
<div class="tab" id="q8" onlick="verificar(this.id);"></div>
<div class="tab" id="q9" onlick="verificar(this.id);"></div>
</div>
First, syntax error. It’s
onclick
, nayonlick
(nobody wants to lick anyone here, either? ), missed onec
in all its elements. Second, logic error, you assign X/Y toteste
which is a local variable in the function, but this will not change your element. You need to do something likedocument.getElementById...innerHTML = teste
– Woss
Even with Anderson’s hints your code will not work as expected. After it leaves the if for the first time will always remain in Else.
– LeAndrade