<!DOCTYPE html>
<html>
<head>
<title>Title of the Document</title>
<script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
</head>
<body>
<div id='divId' style="height: 100px; width: 200px; border: 1px solid red;">
<!-- This comment won't be removed -->
<span>
Welcome to W3Docs <!-- This comment WILL be removed -->
</span>
<!-- But this one won't. -->
</div>
<button id='buttonId'>Remove via lastElementChild-loop</button>
<script>
buttonId.onclick = () => {
const myNode = document.getElementById("divId");
while(myNode.lastElementChild) {
myNode.removeChild(myNode.lastElementChild);
}
}
</script>
</body>
</html>