> But looping over querySelectorAll() is painful.
Not any more:
let elements = document.querySelectorAll('.some-class')
elements.forEach(function(element){
...
})
https://developer.mozilla.org/en-US/docs/Web/API/NodeList/fo...PS. The other two answers here are outdated.