Removing Elements
JS · DOM Manipulationsyntax
element.remove()
parent.removeChild(child)example
// Modern: element removes itself
const notification = document.querySelector(".notification");
notification.remove();
// Remove all children
const container = document.querySelector("#list");
while (container.firstChild) {
container.removeChild(container.firstChild);
}
// Faster: clear all children
container.replaceChildren();Note el.remove() is cleaner than parentNode.removeChild(el). Use replaceChildren() with no arguments to efficiently clear all children.