I'm trying to loop through an array of items and build up a HTML structure based on what count we are at through the array.
It should wrap every item in a cell and every 4 items in a row and every 8 items in a column div.
Here is the JavaScript:
var html = ''; // for those that were asking where html was...
for(var i=0;i<response.length;i++)
{
// for first and every 8 items
if(i == 0 || i % 8 === 0)
{
console.log('columnstart');
html = html + '<div class="column">';
}
// for first and every 4 items
if(i == 0 || i % 5 === 0)
{
console.log('rowstart');
html = html + '<div class="row">';
}
// after every 4 items BUT NOT the first
if(i % 4 === 0 && i !== 0)
{
console.log('rowend');
html = html + '</div>';
}
// after every 8 items BUT NOT the first
if(i == response.length && i !== 0 || i % 7 === 0 && i !== 0)
{
console.log('columnend');
html = html + '</div>';
}
console.log('cell');
html = html + '<div class="cell"></div>';
}
and here is an example of how the HTML should be being rendered:
<div class="column">
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
<div class="column">
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
However it seems my counts are off...
Because I get the following in the console:
columnstart
rowstart
cell
cell
cell
cell
cell
rowend
rowstart
cell
cell
cell
columnend
columnstart
cell
rowend
cell
rowstart
cell
cell
htmldeclared? You should declare it asvar html = ''and then instead ofhtml = html + somethingyou can usehtml += something. It's important that when you declarehtmlyou set it equal to an empty string, otherwise you end up withundefinedat the beginning of your html string which can cause errors when the browser tries to parse it.