I am using JavaScript and jQuery to create a simple 40x40 grid.
Here's my nested for loop to do this:
function display_grid() {
browser_grid = ''
$visible_grid = $('#grid');
for (i = 0; i < 40; i++) {
$visible_grid.append('<div>');
for (i = 0; i < 40; i++) {
$visible_grid.append("<div class='square'> </div>");
}
$visible_grid.append('</div>');
}
}
I expect this to create 40 divs each with 40 divs inside each one of them. The browser shows only one single row with 40 divs.
<div>
<div class="square></div>
<div class="square></div>
<div class="square></div>
...
</div>
This is what I want it to do, but forty times. I'm not very experienced with JS, so I'm confused as to why the first loop isn't executing 40 times.
.append()depends upon, does not allow for injecting partial markup, such as'<div>'and'</div>'at separate times. It will forcibly "fix" the<div>into<div></div>and likely ignore the</div>. If you want to construct markup in parts, use a string and.append()the combined result.