home ~ socials ~ projects ~ rss

Look Through Results From querySelectorAll In JavaScript

January 2025
const els = document.querySelectorAll('.some-class');

else.forEach((el) => {
  // do something
});

or

// NOTE: This one needs to be tested once
// I get my site pages working again

const els = document.querySelectorAll('.some-class');

else.forEach((el) => {
  // do something
});

Notes

  • .querySelectorAll() returns a static NodeList
  • Because it's a static list, changing the DOM to add or remove elements that match the query won't effect the results
  • NodeLists can be looped over with .forEach
  • You can check a NodeList's length with .length
  • NodeLists aren't an array so you can't do a regular for loop on them without converting them to an array.
  • You can convert them to an array with Array.from()
end of line
Share link:
https://www.alanwsmith.com/en/2s/m8/0p/6f/?look-through-results-from-queryselectorall-in-javascript