Here’s some code:
<!Doctype html>
<html>
<title>
Tic Tac Toe!
</title>
<style>
*
{
margin: 0 auto;
padding: 0;
}
html, body
{
position: relative;
width: 100%;
height: 100%;
}
h1
{
position: absolute;
font-family: "Impact";
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
text-align: center;
}
#container
{
position: absolute;
width: 300px;
height: 300px;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
.box
{
width: 33.33%;
height: 33.33%;
background-color: #009900;
cursor: pointer;
}
.box:hover
{
background-color: #008000;
}
.box:active
{
background-image: url("X.png");
}
.rightBox
{
float: right;
}
.midBox
{
float: right;
}
.leftBox
{
float: left;
}
</style>
<body onload = "startGame()">
<h1 id = "turnStatus"></h1>
<div id = "container">
<div id = "box1" class = "box leftBox"></div> <div id = "box3" class = "box rightBox"></div> <div id = "box2" class = "box midBox"></div>
<div id = "box4" class = "box leftBox"></div> <div id = "box6" class = "box rightBox"></div> <div id = "box5" class = "box midBox"></div>
<div id = "box7" class = "box leftBox"></div> <div id = "box9" class = "box rightBox"></div> <div id = "box8" class = "box midBox"></div>
</div>
<script>
function startGame()
{
var boxClass = document.getElementsByClassName("box");
var turnStatus = document.getElementById("turnStatus");
var yourTurn;
function displayTurnMessages()
{
if(yourTurn === true)
{
turnStatus.innerHTML = "PLAYER 1 TURN";
}
else if(yourTurn === false)
{
turnStatus.innerHTML = "PLAYER 2 TURN";
}
}
function shuffleTurn() //whoever goes first will be randomly chosen
{
var turn = Math.floor(Math.random() * 2) + 1; //choose random number between 1 and 2
if(turn === 1) //if 1, your turn
{
yourTurn = true;
}
else if(turn === 2) //if 2, player 2 turn
{
yourTurn = false;
}
}
var offLimitsList = new Array(); //if anyone wants to move to a position in this list, it won't let them
function isOffLimits(selectedDivId)
{
var isOffLimits = false;
for(var i = 0; i < offLimitsList.length; i++)
{
if(offLimitsList[i].id === selectedDivId)
{
isOffLimits = true;
}
}
return isOffLimits;
}
for(var i = 0; i < boxClass.length; i++)
{
boxClass[i].addEventListener("click", function()
{
if(isOffLimits(boxClass[i].id) != true) //if isOffLimits(div.id) != true, change background and background color
{
boxClass[i].style.backgroundColor = "#008000";
boxClass[i].style.backgroundImage = "url(\"X.png\")";
}
});
}
function placePiece() //when called, this should show an x or o in the box
{
if(yourTurn === false)
{
}
}
function alternateTurn()
{
if(yourTurn === true)
{
yourTurn = false;
}
else
{
yourTurn = true;
}
}
function identifyWin()
{
}
}
</script>
</body>
</html>
If you run it in chrome and click one of the divs, it produces an error on line 151 saying that it cannot read property ‘id’ of undefined (as if to say that boxClass[i] is undefined). However, I know that boxClass[i] will be equal to a div (I tested this by using a for loop to print the values to the console). So, why am I getting this error?