v2.0.3

Element Extensions

Last updated


Chainable methods on Element.prototype and DocumentFragment.prototype for content, classes, attributes, children, traversal, and event handlers.

Element


Source: src/prototype/Element.ts
Method / PropertyDescription
el.lengththis.children.length
el.$iIndex of the element among its parent's children
el.$attributesAll attributes as a plain { name: value } object
el._(value) / el.__(value)Append / replace children — strings and numbers become a text node; plain objects are pretty-printed as <br>-joined JSON
el._style(map) / el._data(map) / el._attr(map)Chainable: set multiple style / dataset / attribute key-values from an object
el._class(list)Chainable: add one or more classes (string, comma/space/dot-separated, or array)
el.class_(list) / el.style_(list) / el.data_(list) / el.attr_(list)Chainable: remove classes / clear styles / delete dataset keys / remove attributes
el.$style(key) / el.$data(key) / el.$attr(key)Read a single style property / dataset entry / attribute value
el.class__()Chainable: clears className entirely
el.__class(list)Chainable: replaces the whole class list
el.$$class(list)true only if every class in list is present
el.$$class_(bool, list)Chainable: adds list when bool is falsy, removes it when truthy (or auto-detects current state if bool is omitted)
el.$$data(key, value?) / el.$$attr(key, value?)true if the dataset/attribute value equals value, or is non-empty when value is omitted
el._children(...args)Chainable: appends each argument via _child
el._child(value, before)Chainable: append/insert a child (or array of children) by index or reference Element
el.__child(value)Chainable: replaces children (replaceChildren)
el.$child(value)Query a descendant by a mixed array of selector strings and numeric indices
el.$parent(count) / el.$pre(count) / el.$next(count)Walk count + 1 steps along parentElement / previousElementSibling / nextElementSibling (count defaults to 0, the adjacent node)
el.$sel(selector) / el.$selAll(selector)querySelector / querySelectorAll guarded against an empty selector
el._<event>(fn)Chainable on<event> = fn assignment; see Event Setters for the full list

Event Setters


Each setter assigns fn to the matching on<event> property and returns the element, so handlers chain like any other setter. Assigning replaces the previous handler for that event (it does not call addEventListener).
GroupSetters
Page / focus_load, _scroll, _focus, _blur
Mouse_click, _dblclick, _contextmenu, _mousedown, _mouseup, _mouseenter, _mouseleave, _mouseover, _mouseout, _mousemove
Keyboard / form_input, _change, _keydown, _keyup, _cut, _paste
Touch_touchstart, _touchmove, _touchend, _touchenter, _touchleave, _touchup
Drag and drop_dragstart, _dragenter, _dragover, _dragleave, _dragend, _drop

touchenter, touchleave, and touchup are not events the browser dispatches, so the handlers set by _touchenter, _touchleave, and _touchup are stored on the element but never called. _touchstart, _touchmove, and _touchend fire only on devices that expose touch events.
  1. <button id="btn">click me</button>
  2. <script>
  3. const btn = "btn".$;
  4. btn._click(() => console.log("first"))._click(() => console.log("second"))._touchup(() => console.log("never"));
  5. btn.click();
  6. console.log(typeof btn.onclick, typeof btn.ontouchup);
  7. </script>
Live preview · @pardnchiu/renderjs@2.0.3

DocumentFragment


Source: src/prototype/DocumentFragment.ts
Method / PropertyDescription
fragment.innerHTMLSerializes the fragment's contents via a detached <div>
fragment.lengththis.children.length
fragment._children(...args)Appends each argument as a child
fragment._child(value)Appends a value (or array of values); strings/numbers become text nodes

Example


Chains child, class, and attribute setters on one list, then reads them back.
  1. <ul id="list"></ul>
  2. <script>
  3. const list = "list".$;
  4. list._children("li"._("one"), "li"._("two"), "li"._("three"))
  5. ._class("menu active")
  6. ._attr({ role: "list" });
  7. console.log(list.length, list.$$class(["menu", "active"]), list.$attr("role"));
  8. console.log(list.$child([1]).textContent, list.children[2].$i);
  9. console.log(list.children[0].$next(1).textContent, list.$attributes);
  10. </script>
Live preview · @pardnchiu/renderjs@2.0.3
中文