Search code examples
javascriptdomnodesappendchildcreateelement

Creating and appending a DOM element with the content of another element


My code is as follows

  ;(function(window){
    var $description_window= document.querySelector('.mg_post_description'),
        $headings= document.querySelectorAll('.mg_blog_main_content h3'),
        $paragraph = document.createElement('p');
        for (var j = 0; j < $headings.length; j++) {
            var $headingChildren=$headings[j].cloneNode(true).childNodes;
            $paragraph.appendChild($headingChildren[j]);
       }
       $description_window.append($paragraph);
})(window);

Here what I am trying to do is; to copy the h3 tags of the content box. Then create a paragraph element. Then append the p tags with collected h3 tags. However, I get the following error on running this.

Failed to execute appendChild on Node: parameter 1 is not of type Node.

  ;(function(window){
    var $description_window= document.querySelector('.post_description'),
    
   $headings= document.querySelectorAll('.post_description h3'),
   $paragraph = document.createElement('p');
   
   for (var j = 0; j < $headings.length; j++) {
   
       var $headingChildren=$headings[j].cloneNode(true).childNodes;
            $paragraph.appendChild($headingChildren[j]);
       }
       
       $description_window.append($paragraph);
       
})(window);
.post_description{

  width:200px;
  height:200px;
  background-color:#555;
  position:absolute;
  top:10%;
  right:8%;
  
}

.post_description a {
  
  color:white;
}

.main_body{

  padding-top:40%;

}
<div class="main_body">
    <h3>Testing one</h3>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

  <h3>Testing two</h3>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


  <h3>Testing three</h3>

  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


  <h3>Testing four</h3>

  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

</div>

<div class="post_description"></div>

Could someone please explain why this occurs and how to solve this

Thanks


Solution

  • To achieve expected result, use below option

    function addElem() {
        var mElem = document.querySelector('.mg_post_description')
        var hElems = document.getElementsByTagName('H3');
        var node = document.createElement("P");
        for (var i = 0; i < hElems.length; i++) {
    
            var textnode = document.createTextNode(hElems[i].innerHTML);
            var cloneElem = node.cloneNode(true);
            cloneElem.appendChild(textnode);
            mElem.appendChild(cloneElem);
        }
    };
    
    addElem()
    

    Codepen- https://codepen.io/nagasai/pen/YVEzdJ