Remove all child elements of a DOM node in JavaScript Remove all child elements of a DOM node in JavaScript javascript javascript

Remove all child elements of a DOM node in JavaScript


Option 1 A: Clearing innerHTML.

  • This approach is simple, but might not be suitable for high-performance applications because it invokes the browser's HTML parser (though browsers may optimize for the case where the value is an empty string).

doFoo.onclick = () => {  const myNode = document.getElementById("foo");  myNode.innerHTML = '';}
<div id='foo' style="height: 100px; width: 100px; border: 1px solid black;">  <span>Hello</span></div><button id='doFoo'>Remove via innerHTML</button>