> Side benefit: also no weird $(thing)[0] when you need to use a DOM API, like HTML5 validity or whatever.
But looping over querySelectorAll() is painful. I'm not really a frontend dev, so perhaps I'm doing it wrong, but this is the "easier" way I found when I was writing an app a few weeks ago (in TypeScript):
// Get Array of HTMLElements from querySelectorAll().
let queryAll = (q: string): HTMLElement[] => {
let nodes = document.querySelectorAll(q);
let arr: HTMLElement[] = [];
arr.length = nodes.length;
for (let i = 0; i < nodes.length; i++) {
if (!(nodes[i] instanceof HTMLElement)) {
arr.length--;
continue;
}
arr[i] = nodes[i] as HTMLElement;
}
return arr;
};