Last updated: Mar 5, 2024
Reading time·4 min
To remove a DOM element by id:
document.getElementById()
method.remove()
on the element, e.g. element.remove()
.remove()
method removes the element from the DOM.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box">Box 1</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const element = document.getElementById('box'); element.remove();
We used the document.getElementById() to
select an element by its id
.
The getElementById()
method returns an Element
whose id
matches the
provided string.
id
in the DOM, the method returns null
.You can use the optional chaining (?.) operator to avoid getting an error if the element doesn't exist.
const element = document.getElementById('box'); element?.remove();
If the element
variable stores a null
or an undefined
value, the optional
chaining operator short-circuits instead of calling the remove()
method.
The last step is to use the Element.remove() method to remove the element from the DOM.
If you need to remove the element without removing its children, use this approach instead.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <!-- 👇️ Only remove this div (not children) --> <div id="parent"> <span>Child 1</span> <span>Child 2</span> </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const element = document.getElementById('parent'); element.replaceWith(...element.childNodes);
To remove an element without removing its children, we basically replace the element with its children.
div
element with id
of parent
, without removing the span
elements located inside of the div
.If you need to handle the scenario where the element doesn't exist, use the optional chaining operator (?.).
const element = document.getElementById('parent'); element?.replaceWith(...element.childNodes);
To remove an element from the DOM on click:
click
event listener to the element.remove()
method on the element in the event handler.Here is the HTML for the examples.
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box" style=" background-color: salmon; width: 100px; height: 100px; " > Box 1 </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); box.addEventListener('click', function handleClick(event) { box.remove(); });
We added a click
event listener to the DOM element.
Each time the element is clicked, a function is invoked.
In our handler function, we used the Element.remove() method to remove the element from the DOM.
target
We could have also used the target
property on the event
object to achieve
the same result.
const box = document.getElementById('box'); box.addEventListener('click', function handleClick(event) { event.target.remove(); });
We used the
target property
on the event
object.
The target
property is a reference to the object (element) on which the event
was dispatched.
event.target
gives us access to the DOM element the user clicked.You can console.log
the target
property to see the DOM element which was
clicked by the user.
const box = document.getElementById('box'); box.addEventListener('click', function handleClick(event) { console.log(event.target); event.target.remove(); });
If you click on the div
element and look at your browser's console
, you'll
see the element being logged.
The target
property is very useful when adding events to more generic objects,
e.g. the document
.
Here is an example that removes the clicked DOM element, regardless of which element it is.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div style="background-color: salmon; width: 100px; height: 100px"> Box 1 </div> <div style=" background-color: salmon; width: 100px; height: 100px; margin-top: 10px; margin-bottom: 10px; " > Box 2 </div> <div style="background-color: salmon; width: 100px; height: 100px"> Box 3 </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
document.addEventListener('click', function handleClick(event) { console.log(event.target); event.target.remove(); });
This time we added the event to the document
object.
Each time an element is clicked, we remove it from the DOM, regardless of which element it was.
I've also written an article on how to hide elements by class in JS.
You can learn more about the related topics by checking out the following tutorials: