I have a container div that contains some text and multiple divs all with class 'test', and I am trying to delete all the divs with class 'test' using pure javascript.
Currently, I have a variable containing all the divs with class 'test' which I have iterated through to remove each one.
var divsToRemove = document.getElementsByClassName("test");
for (var i = 0; i < divsToRemove.length; i++) {
divsToRemove[i].remove();
}
<div id="container">
<p>This text should stay</p>
<div class="test">
<p>Testing 1</p>
</div>
<div class="test">
<p>Testing 2</p>
</div>
<div class="test">
<p>Testing 3</p>
</div>
<div class="test">
<p>Testing 4</p>
</div>
</div>
I expected all of the divs with class 'test' to be removed, but only some of them have been removed with this code