Skip to content

Instantly share code, notes, and snippets.

@theking2
Created June 30, 2024 11:36
Show Gist options
  • Save theking2/79cea0ee7bc80e77c0a39d210dae6c89 to your computer and use it in GitHub Desktop.
Save theking2/79cea0ee7bc80e77c0a39d210dae6c89 to your computer and use it in GitHub Desktop.
compact selector
/**
* returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.
* @param {string} selector
* @param {Element} [document] base
* @returns {(NodeList|Element|null)}
*/
const $ = (selector, base = document) => {
let elements = base.querySelectorAll(selector);
return (elements.length == 0) ? null : (elements.length == 1) ? elements[0] : elements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment