Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

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.

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

Usage

javascript
addClassToQuery("highlight", "widgets")