Skip to main content

Command Palette

Search for a command to run...

JavaScript remove elements

Published
1 min read
JavaScript remove elements
C

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.

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

To remove elements we simply need to get them in JavaScript using any technique really.

Let's say 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.

Making it visible can be achieved 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.

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

More from this blog

D

Daily Dev Tips

887 posts

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊