คำอธิบาย
ตั้งค่า innerHTML = '' เพื่อลบ child elements ทั้งหมดทีเดียว
ตัวอย่างโค้ด
<div id='removeAllChildElements' style="height: 200px; width: 200px; border: 1px solid red;">
<span>Javascript</span>
<div>MySQL</div>
</div>
<button id='remove'>Remove the items</button>
<script>
remove.onclick = () => {
const element = document.getElementById("removeAllChildElements");
element.innerHTML = '';
};
</script>
ผลลัพธ์
กดปุ่ม "Remove the items" → ทุกอย่างในกล่องสีแดงหายไป