Saturday, 28 September 2013

Close and open another div after the loop increments by 3 every time

Close and open another div after the loop increments by 3 every time

I have an array. I am looping over that. First what I need is that when
the loop is run first it should already open up a div and add text to it.
Next I want that if a loop has run 3 times then close the previous div and
open a new div.
Code:
var counter = 0;
for (var i = 0; i < tag_array.length; i++) {
counter++;
if (counter == 3) {
counter = 0;
document.write("</div>");
document.write("<div class='span6'>");
} else {
document.write("<div class='span6'>");
}
document.write("<div class='tag'>" + toTitleCase(tag_array[i]) +
"</div>");
}
The above code is not working. I don't know why. Please explain me what I
am doing wrong and how can I fix it?

No comments:

Post a Comment