I'm trying to remove all elements from the DOM of a specific class, after a specific timeout (waiting for an animation to finish).
I've tried using a for loop both on a live list (getElmentsByClassName) as well as in node list (querySelectorAll). In both cases, only one element is removed.
function removeElems() {
elems = document.querySelectorAll('.header');
for (e of elems) {
setTimeout(function() {e.remove();}, 1000);
}
}
<div id="container">
<div class="header">1</div>
<div class="header">2</div>
<div class="header">3</div>
<div class="header">4</div>
<div class="header">5</div>
<div class="header">6</div>
</div>
I'm not sure why it's not working, and am looking for an explanation as to why it's executing in the manner it does and not actually removing all the elements.
Thank you!
(If you're kind enough to offer examples of working code, vanilla JS only please.)