Blog>
Snippets

Deleting a DOM element

Select a specific `<div>` element by its class `.remove-me` and remove it from the DOM.
const elementToRemove = document.querySelector('.remove-me');
// Check if the element exists in the DOM before attempting to remove it
if (elementToRemove) {
  elementToRemove.parentNode.removeChild(elementToRemove);
}
This code selects the first element with the class '.remove-me', checks if it exists, and then removes it from its parent node, effectively deleting it from the DOM.