JS append items into new element

In this example below you will see how to do a JS append items into new element with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by ro6er, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright ro6er ©
  • HTML
  • CSS
  • JavaScript
    <table>
  <tr id="container1" class="scroll-container-body">
    <th class="square red"></th>
    <th class="square green"></th>
  </tr>
</table>

/*Downloaded from https://www.codeseek.co/ro6er/js-append-items-into-new-element-ZowPEM */
    #container{
  border: 1px solid #ccc;
  padding: 2px;
}
.square{
  width: 10px;
  height: 10px;
  margin: 2px;
}
.red{
  background: red;
}

.green{
  background: green;
}
.newContainer {
  background: hotpink;
  padding: 10px;
  margin: 10px;
  display: inline;
  clear: both;
}
tr {
  clear: both;
  width: 100%;
  display: block;
}
th {
  display: inline-block;
}


/*Downloaded from https://www.codeseek.co/ro6er/js-append-items-into-new-element-ZowPEM */
    function duplicateChildNodes (parentId){
  var parent =  document.getElementsByClassName("scroll-container-body")[0];
var lhsCol = document.createElement("div"); 

  NodeList.prototype.forEach = Array.prototype.forEach;
  var children = parent.childNodes;

  children.forEach(function(item){
    var cln = item.cloneNode(true);
    lhsCol.appendChild(cln);
    lhsCol.classList.add("lhs-col");
    parent.append(lhsCol);
  });

};

duplicateChildNodes();

Comments