I have an index.html file that has
<html>
<head>
<script src="2-4.js"></script>
</head>
</html>
and a 2-4.js file that has
var div,
container = document.getElementById('container')
for (var i = 0; i < 5; i++) {
div = document.createElement('div')
div.onclick = function() {
alert('This is box #' + i)
}
container.appendChild('div')
}
They're in the same folder. When I open index.html in Chrome and inspect, I see "Uncaught TypeError: Cannot read property 'appendChild' of null at 2-4.js:11" in the console. I looked up some SO questions involving that error but didn't find them helpful. Does anyone have an idea what's going on? Am I running the program wrong?