This is a really simple question but I don't know why it doesn't work. I have an array with 3 items inside. And I have a container which I would like to insert a number of divs based on the number of items in my array. I used a for loop for this but it is only creating one div. Should it not create 3?
for (var i = 0; i < array.length; i++) {
var container = document.getElementById("container");
container.innerHTML = '<div class="box"></div>';
}
here is a fiddle to demonstrate further fiddle
+=instead of=if you want to add content to thecontainerand not replace it.#containerin every loop step by using=. Instead useappend()or+=. Also, you make unnecessary DOM access in every loop step which is a very expensive operation. Move the definition of yourcontainervariable outside of the loop.+=and that your comment has earned upvotes is unsettling to say the least. Please see this comment and this one.