I am attempting to run this loop for the purpose of creating a specific amount of columns and add squares to each column. The goal was to do this all in one loop but my loop will run once, successfully, then quit.
function gridBuilder(columns,rows) {
var toAdd = document.createDocumentFragment();
for (i = 1; i <= columns; i++) {
var column = document.createElement('div');
column.id = 'card-column-'+i;
for (i = 1; i <= rows; i++) {
var row = document.createElement('div');
row.id = 'card-'+i;
row.className = 'card';
column.appendChild(row);
}
column.className = 'card-column';
toAdd.appendChild(column);
}
document.getElementById('card-container').appendChild(toAdd);
}
Understanding you are missing the HTML/CSS (it's a lot). Why is it that when I run gridBuilder(5,4);
it creates only 1 column with 4 Squares, like below.
<div id="card-container">
<div id="card-column-1" class="card-column">
<div id="card-1" class="card"></div>
<div id="card-2" class="card"></div>
<div id="card-3" class="card"></div>
<div id="card-4" class="card"></div>
</div>
</div>
Note: Chrome console gives no errors. I got most of this from here. Is it not possible to run a nested for loop like this for some reason?