Skip to content

Instantly share code, notes, and snippets.

@theking2
Created June 30, 2024 11:36
Show Gist options
  • Select an option

  • Save theking2/79cea0ee7bc80e77c0a39d210dae6c89 to your computer and use it in GitHub Desktop.

Select an option

Save theking2/79cea0ee7bc80e77c0a39d210dae6c89 to your computer and use it in GitHub Desktop.

Revisions

  1. theking2 created this gist Jun 30, 2024.
    10 changes: 10 additions & 0 deletions selector.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    /**
    * 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;
    }