My code (simplified):
HTML:
<!DOCTYPE html>
<head>
<title>Title</title>
<script type="text/javascript" src="main.js"></script>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body align="center">
<div id="container">
<h1>Title</h1>
</div>
</body>
</html>
JS:
const container = document.querySelector('#container');
for (i = 0; i < 16; i++) {
for (let x = 0; x < 16; x++) {
const cellSize = 512 / 16;
const cell = document.createElement('div');
cell.style.width = cellSize + 'px';
cell.style.height = cellSize + 'px';
container.appendChild(cell);
}
};
Console:
Uncaught TypeError: Cannot read property 'appendChild' of null
What have I done wrong here? It might just be my browser (I'm using Chrome). Is it because the constants are being referenced inside the for loop?