I am currently working on creating a Tic Tac Toe game and have the code to make the x and o’s appear. However, the onclick event isn’t always being called when the squares are clicked, and randomly decide to work after many clicks. What am I doing wrong?
The javascript is at the top and the SVG elements are at the bottom
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script lang="javascript">
let whosTurn = "o";
function clickedSquare(x1,x2, o) {
console.log('hi!')
var eleX = document.getElementById(x1);
var eleXR = document.getElementById(x2)
var eleO = document.getElementById(o);
var eleXstyle = eleX.getAttribute("stroke");
var eleXRstyle = eleXR.getAttribute("stroke");
var eleOstyle = eleO.getAttribute("stroke");
if (eleXstyle == "transparent" && eleOstyle == "transparent") {
if (whosTurn = "x") {
eleXstyle == "black"
eleXRstyle =="black"
eleX.setAttribute( "stroke", "black");
eleXR.setAttribute( "stroke", "black");
whosTurn = "o"
} else {
eleOstyle == "black"
eleO.setAttribute( "stroke", "black");
whosTurn = "x"
}
}
console.log(whosTurn)
}
function checkForWinner() {
}
</script>
</head>
<body>
<svg width="500" height="500" style="background-color: black;" viewBox="0 0 500 500">
<rect width="160" height="160" x="0" y="0" fill="red" onclick="clickedSquare('x1l','x1r','circle1');"></rect>
<rect width="160" height="160" x="170" y="0" fill="white" onclick="clickedSquare('x2l','x2r','circle2'); checkForWinner();"></rect>
<rect width="160" height="160" x="340" y="0" fill="white" onclick="clickedSquare('x3l','x3r','circle3'); checkForWinner();"></rect>
<rect width="160" height="160" x="0" y="170" fill="white" onclick="clickedSquare('x4l','x4r','circle4'); checkForWinner();"></rect>
<rect width="160" height="160" x="170" y="170" fill="white" onclick="clickedSquare('x5l','x5r','circle5'); checkForWinner();"></rect>
<rect width="160" height="160" x="340" y="170" fill="white" onclick="clickedSquare('x6l','x6r','circle6'); checkForWinner();"></rect>
<rect width="160" height="160" x="0" y="340" fill="white" onclick="clickedSquare('x7l','x7r','circle7'); checkForWinner();"></rect>
<rect width="160" height="160" x="170" y="340" fill="white" onclick="clickedSquare('x8l','x8r','circle8'); checkForWinner();"></rect>
<rect width="160" height="160" x="340" y="340" fill="white" onclick="clickedSquare('x9l','x9r','circle9'); checkForWinner();"></rect>
<line x1="20" y1="20" x2="130" y2="120" stroke="transparent" stroke-width="5px" class="exes x1" id="x1l"></line>
<line x1="130" y1="20" x2="20" y2="120" stroke="transparent" stroke-width="5px" class="exes x1" id="x1r"></line>
<line x1="190" y1="20" x2="300" y2="120" stroke="transparent" stroke-width="5px" class="exes x2" id="x2l"></line>
<line x1="300" y1="20" x2="190" y2="120" stroke="transparent" stroke-width="5px" class="exes x2" id="x2r"></line>
<line x1="360" y1="20" x2="470" y2="120" stroke="transparent" stroke-width="5px" class="exes x3" id="x3l"></line>
<line x1="470" y1="20" x2="360" y2="120" stroke="transparent" stroke-width="5px" class="exes x3" id="x3r"></line>
<line x1="20" y1="190" x2="130" y2="290" stroke="transparent" stroke-width="5px" class="exes x4" id="x4l"></line>
<line x1="130" y1="190" x2="20" y2="290" stroke="transparent" stroke-width="5px" class="exes x4" id="x4r"></line>
<line x1="190" y1="190" x2="300" y2="290" stroke="transparent" stroke-width="5px" class="exes x5" id="x5l"></line>
<line x1="300" y1="190" x2="190" y2="290" stroke="transparent" stroke-width="5px" class="exes x5" id="x5r"></line>
<line x1="360" y1="190" x2="470" y2="290" stroke="transparent" stroke-width="5px" class="exes x6" id="x6l"></line>
<line x1="470" y1="190" x2="360" y2="290" stroke="transparent" stroke-width="5px" class="exes x6" id="x6r"></line>
<line x1="20" y1="360" x2="130" y2="470" stroke="transparent" stroke-width="5px" class="exes x7" id="x7l"></line>
<line x1="130" y1="360" x2="20" y2="470" stroke="transparent" stroke-width="5px" class="exes x7" id="x7r"></line>
<line x1="190" y1="360" x2="300" y2="470" stroke="transparent" stroke-width="5px" class="exes x8" id="x8l"></line>
<line x1="300" y1="360" x2="190" y2="470" stroke="transparent" stroke-width="5px" class="exes x8" id="x8r"></line>
<line x1="360" y1="360" x2="470" y2="470" stroke="transparent" stroke-width="5px" class="exes x9" id="x9l"></line>
<line x1="470" y1="360" x2="360" y2="470" stroke="transparent" stroke-width="5px" class="exes x9" id="x9r"></line>
<circle cx="70" cy="70" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle1"></circle>
<circle cx="250" cy="70" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle2"></circle>
<circle cx="430" cy="70" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle3"></circle>
<circle cx="70" cy="250" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle4"></circle>
<circle cx="250" cy="250" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle5"></circle>
<circle cx="430" cy="250" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle6"></circle>
<circle cx="70" cy="430" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle7"></circle>
<circle cx="250" cy="430" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle8"></circle>
<circle cx="430" cy="430" r="60" stroke="transparent" stroke-width="5px" fill="transparent" class="circle" id="circle9"></circle>
</svg>
<script>
</script>
</body>
</html>
2
Answers
The main problem was: in the SVG version 1.1 the rendering order happens based on the order of items. So, in your implementation you can click only in the surrounding area in every rect.
I solved the problem by using the display attribute (equals to null). Which will remove the element from the document. As a result, you can click anywhere inside the rect element.
just for a start: