How to Remove an Element in JavaScript
Use remove() when an HTML element should disappear from the page after a click, validation event, timeout, or UI state change. This is the cleanest way to delete a node from the DOM.
What you’ll build or solve
You’ll learn how to remove an element in JavaScript using the DOM remove() method. You’ll also know when removing is better than hiding.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when the element should no longer exist in the page structure.
Common real-world scenarios include:
- Closing alert banners
- Deleting todo items
- Removing modal overlays
- Clearing temporary loaders
- Dismissing notifications
This is a bad idea when the element may need to come back quickly. In that case, hiding with a class is often better.
Prerequisites
You only need:
- A basic HTML page
- A JavaScript file or
<script>tag - Basic DOM selection knowledge
Step-by-step instructions
Step 1: Select the element and call remove()
Start by selecting the target element.
HTML
<div class="alert">Saved successfully</div>
<script>
const alertBox = document.querySelector(".alert");
alertBox.remove();
</script>
This deletes the node from the DOM completely.
A common use case is removing items after a click.
JavaScript
const item = document.querySelector(".task");
item.remove();
What to look for:
remove()deletes the node completely- The element no longer exists in the DOM
- Better than hiding when it is truly finished
- Great for dismissible UI components
- Use hiding instead if the element may return
Examples you can copy
Remove alert
JavaScript
document.querySelector(".alert").remove();
Delete todo item
JavaScript
document.querySelector(".task-item").remove();
Close modal
JavaScript
document.querySelector(".modal").remove();
Common mistakes and how to fix them
Mistake 1: Trying to remove before selecting
What the reader might do:
JavaScript
alertBox.remove();
Why it breaks: alertBox is undefined if the element was never selected.
Corrected approach:
JavaScript
const alertBox = document.querySelector(".alert");
alertBox.remove();
Mistake 2: Removing when the element should only hide
What the reader might do:
JavaScript
menu.remove();
Why it breaks: rebuilding the element later becomes harder.
Corrected approach:
Use class toggling or display: none when the UI needs to reopen quickly.
Mistake 3: Selecting the wrong repeated element
What the reader might do:
JavaScript
document.querySelector(".task").remove();
Why it breaks: only the first matching task is removed.
Corrected approach:
Select the exact target from the click event or loop through matching nodes.
Troubleshooting
If nothing disappears, confirm the selector matches the correct element.
If the wrong repeated item is removed, use a more specific selector.
If the element needs to come back later, hide it instead of removing it.
If remove() throws an error, check whether the selector returned null.
Quick recap
- Use
remove()to delete DOM nodes - Select the element first
- The node fully leaves the DOM
- Great for alerts and dismissible UI
- Hide instead when the element may return
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot