Agregar Divs

by Jelgab

HTML

<h3>
Agregar Divs
</h3>

<div id = "Resultados" >
  &nbsp;
</div>

<hr/>
<a href = "javaScript:void(0)" onclick = "appendChildren()">append Children</a>

JavaScript

//Agregar Divs
console.clear();

function printResults( theValue ){
   $("#Resultados").append( theValue );
   $("#Resultados").append( $("<br/>") );
}

function appendChildren() {
  var allDivs = document.getElementsByTagName("div");

  var currentNumberOfDivs = allDivs.length;
  for (var i = 0; i < currentNumberOfDivs; i++) { //The error: Altering collection during loop. Before it was like this: i < allDivs.length. Length was increasing each time an element was added.
    var newDiv = document.createElement("div");
    decorateDiv(newDiv);
    allDivs[i].appendChild(newDiv);
  }
}