For many cases we can use
style="display: none". But this is only useful when you would like to hide elements. They will still be present in the DOM (but not rendered by the Browser).
To permanently remove an element from the DOM, we can use the built in
var elementToRemove = document.getElementById('elementId');
var parent = elementToRemove.parentNode; parent.removeChild(elementToRemove);
However, you can verify if the support of browsers is sufficiant for your Use-Case at the website caniuse.
While removing elements from the DOM is straightforward, here are some best practices to ensure your code is efficient and error-free:
Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.