An attempt to write a tic-tac-toe game using JavaScript
P粉733166744
2023-09-03 15:07:48
<p>I'm trying to implement the X's and O's in Tic Tac Toe via JavaScript by clicking a square on my board. I have to create X or O for Tic Tac Toe game. But I'm not sure how to make an X or an O. And I need it to be functional like an actual tic-tac-toe game. </p>
You have to iterate over each square because querySelectorAll returns multiple nodes and you can't add event listeners directly on it because addEventListener can only work on one node at a time.
I've also added some CSS to the code snippet below so you can see it working when you run it. Ignore this in the final project.
Edit: I changed the above code snippet to alternate on click, which can be done by initializing a variable with the current player (X or O) and swapping on click using a simple ternary statement use. I also added functionality to your reset button!