I'm trying to remove all hidden classes from elements on click with JavaScript. Here is the dummy code I use to try doing this:
<style>
.hidden {display:none;}
</style>
<div>Value 1</div>
<div class="hidden">Value 2</div>
<div class="hidden">Value 3</div>
<div class="hidden">Value 4</div>
<button onclick="removeHidden()">Show All</button>
<script>
function removeHidden()
{
var hidden = document.getElementsByClassName("hidden");
for(var i=0; i<hidden.length; i++)
{
hidden[i].classList.remove("hidden");
}
}
</script>
When clicking the button, I would expect all classes 'hidden' to be removed but oddly, it removes the hidden class from the second div and from the fourth but skips the third.
The result I get is:
Value 1
Value 2
Value 4
Any idea why that is because I really don't understand this?
I also tried this code but with the same result:
var els = document.getElementsByClassName("hidden");
Array.prototype.forEach.call(els, function(el) {
el.ClassList.remove("hidden");
});