I am trying to create a page that creates 5 random DIVS using javascript. For some reason it only works when I have any content before the <!doctype html> tag on my page which makes no sense to me. Any insight as to what I am doing wrong?
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div id = "boxHouse">
</div>
<script>
var divNum = 1;
for (x = 0; x<5; x++){
var boxDiv = document.getElementById("boxHouse");
var newElm = document.createElement("div");
newElm.id = divNum;
newElm.style.width = Math.floor((Math.random()*100)+2);
newElm.style.height = Math.floor((Math.random()*100)+2);
newElm.style.border = "thin solid black";
newElm.style.backgroundColor = "#FF0000";
divNum++
boxDiv.appendChild(newElm);
}
</script>
</body>
</html>