The remove() method in Javascript

The ‘remove()` method allows you to remove an element from the page without having to access its parent. This is the most convenient way to delete elements in modern JavaScript.

element.remove();

Deleting an item from a page by id

let element = document.getElementById('box');
element.remove();
// Completely removing the element with the id "box"

Deleting an element by class

document.querySelector('.item').remove();

Deleting all list items, using a combination with forEach()

document.querySelectorAll('.list-item').forEach(item => item.remove());

Removing elements from a page after events, such as when a wedge is placed on something

document.getElementById('deleteBtn').addEventListener('click', function() {
this.remove();
// The button will delete itself
});

The remove() method helps with removing elements from the DOM. It gets rid of unnecessary code and makes working with dynamic interfaces more convenient.


Home About Links

Text me