CRELT DOM Element Creation Utility

1.0.6 · active · verified Sun Apr 19

CRELT is a minimalist and tiny JavaScript utility designed for the efficient creation of DOM elements. It simplifies the process of programmatically constructing HTML structures by providing a single function that accepts a tag name (or existing DOM element), an optional attributes object, and any number of child nodes. Attributes specified as strings are set via `setAttribute`, while non-string values (like functions for event handlers) are assigned as direct properties. Children can be strings (for text nodes), DOM nodes, or arrays of children. The library is currently stable at version 1.0.6, indicating a mature and likely infrequent release cadence. Its key differentiator is its small footprint and direct, unopinionated approach to DOM manipulation, serving as a lightweight alternative to JSX or more complex templating engines for scenarios requiring programmatic DOM construction.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a hierarchical DOM structure using `crelt`, including dynamic attributes, event handlers, and different types of children (strings, other `crelt`-generated elements, and arrays of elements).

import crelt from 'crelt';

// Create a main container div
const container = crelt('div', { id: 'app-container', style: 'padding: 20px; border: 1px solid #ccc; font-family: sans-serif;' });

// Create a heading
const heading = crelt('h1', 'Welcome to CRELT Example!');

// Create a paragraph with some text
const paragraph = crelt('p', { class: 'info-text', style: 'color: #555;' },
  'CRELT is a tiny utility for creating DOM elements. ',
  'It simplifies the process of building complex structures directly in JavaScript.',
);

// Create a button with an event handler
let clickCount = 0;
const statusSpan = crelt('span', { id: 'status-message', style: 'margin-left: 10px; font-weight: bold; color: #007bff;' }, 'Not clicked yet.');

const button = crelt('button',
  {
    onclick: () => {
      clickCount++;
      statusSpan.textContent = `Button clicked ${clickCount} times!`;
    },
    style: 'padding: 10px 15px; background-color: #007bff; color: white; border: none; cursor: pointer; border-radius: 4px;',
  },
  'Click Me!'
);

// Combine button and status span into a div
const buttonContainer = crelt('div', { style: 'margin-top: 20px;' }, button, statusSpan);

// Create a list of items using an array for children
const items = ['Item A', 'Item B', 'Item C'];
const list = crelt('ul',
  { style: 'margin-top: 20px; padding-left: 20px;' },
  items.map(item => crelt('li', { style: 'margin-bottom: 5px;' }, item))
);

// Append all elements to the container
crelt(container, heading, paragraph, buttonContainer, list);

// Append the main container to the document body
document.body.appendChild(container);

console.log('DOM structure created and appended using CRELT.');

view raw JSON →