Frontend Engine

Dom.js Core Documentation

A lightweight, zero-dependency ES module offering fluent DOM manipulation, event delegation, and micro-reactive state patching.

Get Dom.js

Standalone ES Module • ES2022 Target • ~4KB Gzip

Download dom.min.js Unminified Source

Quick Start

Import Dom.js into your frontend project using modern ES module syntax:

import { dom, dm } from './js/HuchiOS/Dom.js';

// Instantiate wrapper around dynamically created HTML
const card = dom('<div class="card"></div>')
  .append('<p data-x-ref="status">Standing By</p>')
  .appendTo('#app');

// Event Delegation & Micro-Reactivity
card.on('click', () => {
  card.patch({ status: 'Updated via .patch()' });
});

Live Sandbox Demo

Click the trigger button below to evaluate state patching via .patch() in real time:

$1,250.00

Status: Standing By

Exhaustive API Reference

Initialization & Factory

Function / Method Parameters Description
dom(selector, context) / dm(...) selector: String | HTMLElement | Dom | Document
context: HTMLElement | Dom | Document
Global factory function instantiating a new Dom wrapper.
Dom.init(selector, context) selector: String | HTMLElement | Dom | Document
context: HTMLElement | Dom | Document
Static initializer returning a clean wrapper instance.

Node Unwrapping & Traversal

Method Parameters Description
.getRawElement(index) index: Number (default: 0) Returns the raw native HTMLElement at specified index.
.node(index) index: Number (default: 0) Retrieves underlying Node or HTMLElement at index.
.nodeList() none Returns encapsulated raw NodeList, HTMLElement, or Document.
.length none (Getter) Returns node collection count (1 for single element, N for lists).
.use(index) index: Number (default: 0) Returns a new Dom instance wrapping the element at specified index.
.useFirst() / .useLast() none Wraps the first or last element in the selection.
.parent(index) index: Number (optional) Navigates up ancestor tree depth and returns raw HTMLElement.
.useParent(index) index: Number (optional) Returns a Dom instance wrapping ancestor at target depth.
.child(index, includeWhitespace) index: Number
includeWhitespace: Boolean
Returns raw child nodes excluding text/comments by default.
.useChild(index) index: Number Wraps child node at index into a Dom instance.
.find(selector) selector: String Scope-queries child descendants matching CSS selector.
.closest(selector) selector: String Ascends tree to closest matching parent (Element.closest wrapper).
.lookup(search, index) search: String | Object
index: Number
Deep-scans ancestor chain matching tag, class, ID, or attributes.

DOM Manipulation & Structural Modifiers

Method Parameters Description
.append(node) / .prepend(node) node: Dom | HTMLElement | String Injects element or raw HTML structure into container.
.appendTo(target) / .preppendTo(target) target: Dom | HTMLElement | String Appends or prepends current element into target container.
.before(node) / .after(node) node: Dom | HTMLElement | String Injects element adjacent to current node.
.empty() none Wipes all child nodes and innerHTML content.
.remove() none Detaches element from DOM tree and returns parent Dom instance.
.inner(html) / .html(content) html: String | Dom Gets or sets innerHTML content.
.outer(html) html: String Gets or sets outerHTML structure.
.text(content) content: String Gets or sets textContent.

Attributes, Classes & Styles

Method Parameters Description
.addClass(...klasses) klasses: String | Array<String> Adds CSS classes; flattens space-separated strings.
.removeClass(...klasses) klasses: String | Array<String> Removes specified CSS classes.
.replaceClass(oldK, newK) oldK: String, newK: String Replaces target class with new string.
.toggle(c1, c2) c1: String, c2: String Toggles between two class names or adds c1.
.hasClass(klass) klass: String Checks if element contains target class.
.attr(attr, value) attr: String | Object, value: Any Gets/sets attributes; accepts key-value pairs or object maps.
.removeAttr(attr) attr: String Removes specified attribute.
.data(name, value) name: String, value: Any Gets/sets data-* attributes.
.css(style, value) style: String | Object, value: Any Gets or sets computed inline styles.
.opacity(val) / .fade(x) val/x: Number Sets opacity supporting both 0.0-1.0 and 1-100 numeric scales.
.show(type) / .hide() type: 'inline' | 'flex' | 'block' Toggles display and visibility rules simultaneously.

Events, Forms & Micro-Reactivity

Method Parameters Description
.on(action, callback) action: String, callback: Function Binds delegated event listeners via integrated Binder wrapper.
.off(action, callback) action: String, callback: Function Detaches active event listeners.
.trigger(action) action: String Dispatches custom or native bubbling DOM events.
.serialize() none Serializes <form> inputs into key-value JS object.
.options(map) map: Object Repopulates <select> options from key-value pair map.
.selectOn(search, isText) search: String, isText: Boolean Selects option matching value or visible inner text.
.patch(data) data: Object Updates elements matching data-x-ref/data-x-key with pulse transitions.
.each(callable) callable: Function(DomInstance, index) Iterates over collection, passing wrapped instances to callback.

Production Code Examples

1. Dynamic Form Handling & Ajax Payload Serialization

Capture form submit events, extract plain object payloads, and manage form UI disabled states during async network requests:

import { dom } from './js/HuchiOS/Dom.js';

// Bind submit event handler
dom('#login-form').on('submit', async (e) => {
  e.preventDefault();
  
  const form = dom(e.target);
  
  // Convert inputs directly to key-value object: { email: "...", password: "..." }
  const payload = form.serialize();
  
  // Disable form inputs during processing
  form.find('input, button').attr('disabled', 'disabled');
  
  try {
    const response = await fetch('/api/login', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(payload)
    });
    console.log('Server response:', await response.json());
  } finally {
    form.find('input, button').removeAttr('disabled');
  }
});

2. Real-Time Dashboard Updates via .patch()

Target UI nodes tagged with data-x-ref or data-x-key attributes to execute atomic DOM updates with visual pulse feedback:

import { dom } from './js/HuchiOS/Dom.js';

// Select component container holding data-x-ref tags
const metricsWidget = dom('#financial-metrics');

// Handle WebSocket update payload
function onMetricsReceived(data) {
  // Automatically updates content and triggers 'patch-pulse' CSS transition
  metricsWidget.patch({
    revenue: `$${data.revenueTotal}`,
    activeUsers: data.userCount,
    status: data.isLive ? 'Active' : 'Offline'
  });
}

3. Component Context Traversal via .lookup()

Perform deep ancestor traversal to identify parent containers matching specific attributes or tags during event delegation:

import { dom } from './js/HuchiOS/Dom.js';

// Handle delegate clicks across a large data table
dom('#data-table').on('click', (e) => {
  const clicked = dom(e.target);
  
  // Deep-scan parent tree for container holding 'data-item-id' attribute
  const row = clicked.lookup({ 'data-item-id': true });
  
  if (row) {
    const itemId = row.data('item-id');
    console.log(`Action initiated on dataset row #${itemId}`);
  }
});