function getDomElement(id, removeChildren=false) { let element = document.getElementById(id); if (removeChildren) { try { while (element.firstChild) { element.removeChild(element.firstChild); } } catch {} } return (element); } code example
Example: js remove child elements
function deleteChild() {
let e = document.querySelector("ul");
let child = e.lastElementChild;
while (child) {
e.removeChild(child);
child = e.lastElementChild;
}
}
let btn = document.getElementById(
"btn").onclick = function() {
deleteChild();
}