Q:

Add a variable to href in a 'for' loop

Here is the code:
for (i = 0; i < 8; i++) {
    var meun = document.createElement("div");

    meun.setAttribute('id', i);
    meun.setAttribute("style", "width:70px; height:70px; background:url(archive/" + i + ".jpg); background-size: 70px 70px; float:left; margin-top:10px; opacity:1");
    document.body.appendChild(frame);
    document.getElementById("frame").appendChild(meun);

    meun.addEventListener('click', function () {
        window.location.href = "i+'.html'"
    }, false);
}

The problem I have is in addEventListener: how to pass the variable i into the href? I guess it is a problem of syntax.

A:

Not only a syntax problem, you also need a closure:
(function(x) {
    meun.addEventListener('click', function(){window.location.href = x + '.html'}, false);
})(i);

