Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

A Utility To Add CSS Classes To Elements That Match A Query

This function takes a class name and a query string. It applies the former to any element on the page that matches the later. There's nothing special about it other than it prevents having to write the body of the function countless times.

Code

function addClassToQuery(className, query) {
  let els = document.querySelectorAll(query);
  for (let i = 0; i < els.length; i ++) {
    els[i].classList.add(className)
  }
}

Code

addClassToQuery("highlight", "widgets")