v0.6.22

Global Helpers

Last updated


This page covers _ and $, the two DOM helpers QuickUI attaches to the global object when it loads.

$(selector)


Returns a single element, HTMLElement | null.
  1. <div id="app" class="card">
  2. <input name="email" />
  3. <button>Send</button>
  4. </div>
  5. <script>
  6. console.log($("#app").id);
  7. console.log($("app").id);
  8. console.log($(".card").className);
  9. console.log($("input[name=email]").name);
  10. console.log($("button").textContent);
  11. </script>
Live preview · @pardnchiu/quickui@0.6.22

InputLookup
Starts with #getElementById with # removed
Contains ., [ or ]querySelector
Anything elsegetElementById first, then querySelector

Only the first match is returned; use document.querySelectorAll when you need several.

_(selector, attributes?, children?)


Creates an element from CSS selector syntax.
  1. <script>
  2. const card = _("div#card.box.shadow", { title: "Card", color: "red" }, [
  3. _("h2", "Title"),
  4. "plain text",
  5. ]);
  6. const img = _("img", "/logo.png");
  7. const text = _("temp", "text only");
  8. document.body.append(card);
  9. console.log(card.outerHTML);
  10. console.log(img.outerHTML);
  11. console.log(text.textContent);
  12. </script>
Live preview · @pardnchiu/quickui@0.6.22

ArgumentDescription
selectortag#id.class; must start with a tag or undefined is returned; temp creates a DocumentFragment
attributesAn object, see the table below
childrenA string, number or array; with a single argument, a string / number / array is treated as children and an object as attributes

attributes keyWritten as
value, innerText, innerHTML, textContent, contentEditableElement property
color, backgroundColor, width, height, display, floatstyle
Anything elsesetAttribute; skipped when the value is null or undefined

children typeResult
String or numberWritten to innerHTML; img and source get src instead; temp creates a text node
ArrayAppended in order; strings and numbers become text nodes, Element values are appended as is, anything else is skipped

String children are written with innerHTML, so never pass unescaped user input.
中文