Removing Elements in JavaScript
From the DOM Manipulation cheat sheet · Adding & Removing · verified Jul 2026
Removing Elements
Remove elements from the DOM or replace them with new ones
javascript
// Remove element (modern)
element.remove()
// Remove child (classic)
parent.removeChild(child)
// Replace element
oldElement.replaceWith(newElement)
parent.replaceChild(newElement, oldElement)
// Clear all children
parent.innerHTML = ''
parent.textContent = ''💡 remove() is simpler than removeChild()
⚠️ innerHTML = "" removes event listeners
📌 replaceWith() can replace with multiple items
✅ Always check element exists before removing