Subscribe

JavaScript remove elements

โœ๏ธ

How to remove element in JavaScript

22 Apr, 2021 ยท 1 min read

As we created new elements in JavaScript yesterday, I thought I'd be a good article for today to remove some aspects from the DOM.

We simply need to get them in JavaScript using any technique to remove elements.

We want to remove a div with the ID custom_id.

const elem = document.getElementById('custom_id');
elem.remove();

And yes, that's how simple it is!

Hiding elements in JavaScript

But perhaps you only want to hide it for a brief moment? We can also modify the script to make the element invisible for a while.

const hidden = document.getElementById('hidden_id');
hidden.style.display = 'none';

And that will make your element hidden until you make it visible again.

It can be made visible by swapping the display to block/flex/inline.

const hidden = document.getElementById('hidden_id');
hidden.style.display = 'block';

Feel free to check this out on Codepen.

See the Pen JavaScript remove elements by Chris Bongers (@rebelchris) on CodePen.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

TypeORM viewEntity

30 Mar, 2022 ยท 5 min read

TypeORM viewEntity

The Record Utility Type in TypeScript

12 Mar, 2022 ยท 3 min read

The Record Utility Type in TypeScript

Join 1371 devs and subscribe to my newsletter