Skip to content Skip to sidebar Skip to footer

How To Appendchild(element) Many Times. (the Same Element)

My question is: Is that possible to add the same element without rewriting the same variable. I am creating a slider, and i need to append a div with a class slide-el into block sl

Solution 1:

appendChild will remove the node from wherever it is before appending it to its new location, so you need to make copies of the node instead. You can use cloneNode for that. The true makes cloneNode perform a deep clone, i.e. with all its child nodes.

for(var i = 0; i < urls.length; i++){
  sliderBody.appendChild(slide.cloneNode(true));
}

Solution 2:

Okey guys! I found an answer. I have to put

slide = _createEl("div");
slide.className += "slide-el";

into for loop. Now it looks like this:

for(var i=0; i < urls.length; i++){
  slide = _createEl("div");
  slide.className += "slide-el";
  sliderBody.appendChild(slide);
}

Post a Comment for "How To Appendchild(element) Many Times. (the Same Element)"