A good[ish] website
Web development blog, loads of UI and JavaScript topics
This post examines all the ways multiple DOM elements can be manipulated. That can be done with a while
loop or with recursion when using a live HTMLCollection.
A common issue for the commando coder is to manipulate multiple elements, it requires usually a loop, but not necessary if going the purely functional way by using recursion.
Here's the manipulated HTML:
<div class="something active">foo</div>
<div class="something active">bar</div>
<div class="something active">baz</div>
<div class="something active">fooz</div>
<button class="button">Deactivate</button>
We want to remove those .active
classes when the "Deactivate" button is clicked.
First let's use the good old for
loop as a starting point.
var els = document.querySelectorAll('.something.active')var button = document.querySelector('.button')
button.addEventListener('click', _removeClasses)function _removeClasses() { for (var i = 0; i < els.length; i++) {
els[i].classList.remove('active')
}
}
That's not too bad, but we can do better with Document.getElementsByClassName()
to fetch the elements, it returns an array-like object that is a live HTMLCollection
.
An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.
The below while
loop will illustrate the live nature of HTML collections better.
// This gives us a live an `HTMLCollection`
var els = document.getElementsByClassName('something active')
var button = document.querySelector('.button')
button.addEventListener('click', _removeClasses)
function _removeClasses() {
while (els[0]) {
els[0].classList.remove('active')
}
}
Breakdown:
document.getElementsByClassName()
while (els[0])
els[0].classList.remove('active')
Whenever there is a while
loop, recursion can be used instead.
As a reminder: recursion happens when a function calls itself. Here the _removeClasses
function calls itself after the if (els[0])
stop condition:
var els = document.getElementsByClassName('something active')
var button = document.querySelector('.button')
button.addEventListener('click', _removeClasses)
function _removeClasses() {
els[0].classList.remove('active')
if (els[0]) _removeClasses()
}
Recursive function can be broken into 3 parts:
els[0].classList.remove('active')
if (els[0])
_removeClasses()
For me this was an eye opener in HTMLCollection
s. Hope it also helped you, drop a comment if you have something to add.
Comments would go here, but the commenting system isn’t ready yet, sorry.