I am new to all this javascript, I was trying to make a crossword. I want a webpage that automatically makes 485 divs inside another div when the page loads. How is it possible to do this with a for loop?
Asked
Active
Viewed 2,171 times
-5
-
http://stackoverflow.com/questions/15741006/adding-div-element-to-body-or-document-in-javascript and http://www.w3schools.com/js/js_loop_for.asp – Sam Holder Jul 25 '14 at 14:15
2 Answers
4
I can see you are new to Stackoverflow as well as Javascript. So...I'll cut you some slack and answer (com'n guys...give a new guy a break).
For future questions, I highly recommend checking out https://stackoverflow.com/help/how-to-ask. It'll help you get prompt, accurate answers. Welcome to Stackoverflow.
http://jsbin.com/nazuwoqe/1/edit
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="myContainer"></div>
<script>
var element;
var body = document.getElementById('myContainer');
for(var i=0; i<485; i++) {
element = document.createElement("div");
element.innerHTML = i;
body.appendChild(element);
}
</script>
</body>
</html>
A crossword puzzle isn't going to be a simple project to create. I recommend starting with some more basic tasks and tutorials.
-
1Point for you, but its hard to give someone slack when its an insanely easy question to Google. – Gimby Jul 25 '14 at 14:28
0
Try this
var BigDiv = document.createElement('div');
// specify BigDiv's param like id, class etc. here
var TinyDiv;
for (i = 0; i < 485; i++) {
TinyDiv = document.createElement('div');
// specify TinyDiv's param like id, class etc. here
BigDiv.appendChild(TinyDiv)
}
//put your BigDiv where you want to, here it's in body
document.getElementsByTagName('body')[0].appendChild(BigDiv);

Crow EH
- 86
- 5
-
1
-
I thought you *was* supposed to use `var` in a loop?? https://stackoverflow.com/a/5717233/2826653 – Ben Clarke Jan 14 '19 at 18:15