Skip to content Skip to sidebar Skip to footer

Html Dom Createelement In For Loop

when i want to addChild in the html with Dom,i want to insert some in it, but i confused in the for loop. First time i try this:

Solution 1:

Because br is a unique element. When you use appendChild it removes the element from where it was, and inserts it somewhere else. In addition to creating a new node everytime like in your second example, what you can do is clone the node:

var br = document.createElment("br")
var wh = document.getElementById("whitespaces");
for (var count = 1; count < 11; count++){
    wh.appendChild(br.cloneNode());
}

Post a Comment for "Html Dom Createelement In For Loop"