You can convert it to an array using the slice method from the Array prototype:
var elList = document.querySelectorAll('.viewcount'); elList = Array.prototype.slice.call(elList, 0);
Also, if all you need is forEach , you can call this from an Array prototype without first having to resort to an array:
var elList = document.querySelectorAll('.viewcount'); Array.prototype.forEach.call(elList, function(el) { console.log(el); });
In ES6, you can use the new Array.from function to convert it to an array:
Array.from(elList).forEach(function(el) { console.log(el); });
Currently, this is only in short-term browsers, but if you are using the polyfill service , you will have access to this function through the board.
If you are using the ES6 transporter , you can even use for..of instead:
for (var element of document.querySelectorAll('.some .elements')) {
Joseph Silber Sep 18 2018-11-11T00: 00Z
source share