For-loop example

For JavaScript beginners

by abuhamzah

HTML

<p>The script is a loop that makes a box each time the loop runs. 
When the variable <em>i</em> is an even number, the box color is changed from the default color that is in the CSS (using modulus).</p>

JavaScript

// try changing the number to 100 

for (i = 1; i <= 100; i++) {
    var x = document.createElement("div");
    x.setAttribute("id", "content");
        // passed text into the input
    x.setAttribute("innerHTML", "<p>this is a test</p><p>one moer test</p>");
    document.body.appendChild(x);
}