Javascript DOM How to - Remove child by removeChild method








Question

We would like to know how to remove child by removeChild method.

Answer


<!DOCTYPE html>
<html>
<body>
  <div id="container">
    <div>blablabla....</div>
    <div>blablabla....</div>
  </div><!--   w w w.ja v  a2s .c o  m-->
<script type='text/javascript'>

    var elm=document.getElementById('container');
    var chl=elm.firstChild;
    while(chl)
    {
          if(chl.nodeType==1)
          {
              console.log(elm.removeChild(chl));
              break;
          }
         chl=chl.nextSibling;
    }

</script>
</body>
</html>

The code above is rendered as follows: