dynamically remove the divs that are added earlier dynamically
<p>I wrote a function to insert divs dynamically into the web page. but then I have to remove these divs. While adding the divs I used a classname to all the divs. So I got all the elements from the dom using getElementsByClassName() and looping through all these elements and deleting the divs. My code is not removing all the divs that were created earlier.</p>

<p>Please find my code snippet:</p>

<pre><code>var elements = document.getElementsByClassName("xxxx");

for (var i = 0; i &lt; elements .length; i++) {

  var element = elements[i];

  if (element &amp;&amp; element.hasChildNodes()) {

      var parent_node = element.parentNode;

      while(element.firstChild) {

          parent_node.insertBefore(element.firstChild, element);
      }

      parent_node.removeChild(element);

   }

   elements = document.getElementsByClassName("xxxx");

 }
</code></pre>
