JavaScript: remove()

The Element.remove() method removes an element from the document.

What you will learn

Minimal example

<ul id="list">
  <li id="remove-me">Remove this item</li>
</ul>
const item = document.querySelector('#remove-me');
if (item) item.remove();

The selected li is removed from its parent list. The variable still refers to the removed node, but it is no longer connected to the document.

Common mistakes