0
I’m new to JS and I’m learning, and I’m doing an old game in which the first click is for the player1 to put the "x", and two clicks for the player2 to put the "o", what’s the best way to do it? because I can’t add an "onclick" or an "ondblclick" to the tag, I was trying to make addeventlistener by taking the class, but I couldn’t make it functional.
I am using a table to create the board: updated js code.
function testejogada(){
alert("funcionou");
}
function testejogada1(){
alert("funcionou1");
}
document.addEventListener('DOMContentLoaded', function(){
var classe = document.querySelector(".espaco");
classe.addEventListener('dblclick', function(){ testejogada()
});
classe.addEventListener('click', function(){ testejogada1()
});
});
<table align="center">
<tr>
<td><img class="espaco" src="img/default.png" id="casa1-1" /></td>
<td><img class="espaco" src="img/default.png" id="casa1-2" /></td>
<td><img class="espaco" src="img/default.png" id="casa1-3" /></td>
</tr>
<tr>
<td><img class="espaco" src="img/default.png" id="casa2-1" /></td>
<td><img class="espaco" src="img/default.png" id="casa2-2" /></td>
<td><img class="espaco" src="img/default.png" id="casa2-3" /></td>
</tr>
<tr>
<td><img class="espaco" src="img/default.png" id="casa3-1" /></td>
<td><img class="espaco" src="img/default.png" id="casa3-2" /></td>
<td><img class="espaco" src="img/default.png" id="casa3-3" /></td>
</tr>
<tr>
<td colspan="3" id="ganhador"></td>
</tr>
</table>
Post also code . js please...
– DiegoSantos
in this case I was trying to use something like this: /////////// Function testejogada(){ Alert("worked"); } var Elements = Document.querySelectorAll('.espaco'); for (i = 0; i < Elements.length; ++i) { Elements[i]. addeventlistener('click', test played); }
– montedo-dev
Post the code that writes X and O in the boxes.
– Sam
in case my problem is not how to get the X or O and yes to take the 1 click or 2 clicks and be able to do two different operations, I still do not know how to do this and everything I tried is at least flawed.
– montedo-dev
Post your js so that we can analyze it and understand its logic. Then we will help you correct. We will act on the failures only
– DiegoSantos
then in the above comment I posted in a way I tried to do; --> code ///// Function testejogada(){ Alert("worked"); } var Elements = Document.querySelectorAll('. espaco'); for (i = 0; i < Elements.length; ++i) { Elements[i]. addeventlistener('click', test played); } /////
– montedo-dev
edited the post with the code in js
– montedo-dev
Is double click a prerogative? Because there are simpler ways to do this
– Diego Marques
yes, you have to use the double click, in case it is for learning, I managed to "fix" my code, but I need to put a "team" to differentiate the two clicks, I will edit there in the post, but tbm do not know how to do this "team".
– montedo-dev
@pdm, you should not use dbclick and click on the same element in these cases. Even if you manage to set the Switch, it will always click. There won’t be time to fire the second event after the first one, you understand? The following ref: https://stackoverflow.com/questions/7897558/listen-to-double-click-not-click
– DiegoSantos
truth is pretty bad to use, there would be some other way to do it?
– montedo-dev
@pdm, an alternative is to do as follows: https://codepen.io/valdeir2000/pen/pLqMmM
– Valdeir Psr
@Valdeirpsr thanks seeing your code I managed to have another view of how to use the js, but this way I was able to do in the case with 1 click.
– montedo-dev