remove child element javascript code example
Example 1: delete all childs in node
while (myNode.firstChild) {
myNode.removeChild(myNode.lastChild);
}
Example 2: how to remove an element from a parent element javascript
function removeElement(el) {
el.parentNode.removeChild(el);
}
HTMLElement.prototype.remove = function() { this.parentNode.removeChild(this); return this; }
Example 3: 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();
}
Example 4: js remove all child elements from html
document.getElementById("myId").innerHTML = '';
Example 5: javascript remove last child element
function remove_last_child() {
var select = document.getElementById('parent_element');
select.removeChild(select.lastChild);
}
Example 6: remove parent element javascript
node.parentNode.parentNode.removeChild(node.parentNode)