How to remove elements by class name with JavaScript?

Sometimes, we want to remove elements by class name with JavaScript.

In this article, we’ll look at how to remove elements by class name with JavaScript.

How to remove elements by class name with JavaScript?

To remove elements by class name with JavaScript, we can use the remove method.

For instance, we write

document.querySelectorAll(".user-info").forEach((el) => el.remove());

to select all the elements with class user-info with querySelectorAll.

Then we call forEach on the node list with a callback to call el.remove to remove each element from the DOM.

Conclusion

To remove elements by class name with JavaScript, we can use the remove method.