CRELT DOM Element Creation Utility
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
-
TypeError: crelt is not a function
cause Attempting to import `crelt` using named import syntax (e.g., `import { crelt } from 'crelt';`) in an ES Module context, or incorrect destructuring in CommonJS.fixUse `import crelt from 'crelt';` for ES Modules or `const crelt = require('crelt');` for CommonJS, as `crelt` exports a single default function. -
Uncaught ReferenceError: crelt is not defined
cause The `crelt` function was not imported or defined in the current scope before being used.fixEnsure `import crelt from 'crelt';` (or the equivalent CommonJS `require`) is at the top of your module or script file where `crelt` is being used. -
TypeError: Cannot set properties of undefined (setting 'onclick')
cause This error can occur if `crelt` is called with an invalid first argument that is not a string (tag name) or an existing DOM element, leading to `crelt` returning `undefined` or a non-element, and then trying to set a property on it.fixVerify that the first argument passed to `crelt` is always a valid HTML tag name string (e.g., `'div'`, `'button'`) or a pre-existing DOM element.
Warnings
- gotcha CRELT distinguishes between string values for attributes (using `setAttribute`) and non-string values (using direct property assignment). Passing a string for an event handler like `onclick` will not work, as it will literally set the `onclick` attribute to a string, not assign a function to the property.
- gotcha Null or `undefined` values provided for attributes will be ignored and not set. Similarly, null or `undefined` elements within the children arguments or arrays of children will be skipped. While often convenient, this can mask issues if `null` is passed unintentionally.
- gotcha CRELT's primary use case is creating single DOM elements. While it allows arrays for children, it does not directly support creating DocumentFragments or multiple root-level elements from a single call in the way some templating systems might.
Install
-
npm install crelt -
yarn add crelt -
pnpm add crelt
Imports
- crelt
import { crelt } from 'crelt'import crelt from 'crelt'
- crelt
const { crelt } = require('crelt')const crelt = require('crelt')
Quickstart
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.');