Categories
JavaScript Answers

How to remove elements by class name with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *