Javascript DOM How to - Remove all the DOM elements with a specific tag name








Question

We would like to know how to remove all the DOM elements with a specific tag name.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--from  w  w  w .  j ava  2s.  co  m-->
<body>
  <div>one</div>
  <div>
    two
    <div>three</div>
  </div>
  <div>four</div>
  <script>
Array.prototype.slice.call(document.getElementsByTagName('div')).forEach(function(item) { 
   item.parentNode.removeChild(item); 
} );
</script>
</body>
</html>

The code above is rendered as follows: