TypeScript icon, indicating that this package has built-in type declarations

1.5.11 • Public • Published

DOM Elements factory

Utility to create DOM elements and fragments easily.

Sample usage:

import {a, b, br, div} from "dom-element-factory"

const subDOM = div({classList: {foo: true, bar: false}},
    div(null, [
        a({href: '', click: e => e.preventDefault()}, 'Click me'),
        b({style: {backgroundColor: 'silver'}}, 'Bold text')


/** This code creates such elements under BODY:
 * <div class="foo">
 *     <div>
 *         <a href="">Click me</a>
 *         <br>
 *         <b style="background-color: silver;">Bold text</b>
 *     </div>
 * </div>
 * width click event listener on the link

How to run


npm install dom-element-factory

Usage in a browsers

To run the code from above directly in browser, you'll need to change import statement to this:

import {a, b, br, div} from "./node_modules/dom-element-factory"

Usage under Node.js

To use this utility under Node.js, you should mark your code as module. You can add to package.json according line:

  "type": "module"

To run code sample from above, change import to this code:

import * as DOMElementFactory from "dom-element-factory"
const {a, b, br, div} = DOMElementFactory

Usage in browsers in-place

You can add script tag and import factory like this:

<script type="module">
    import createElement from ''
        createElement('span', null, 'Here we are!')


Create Elements

The main function is the createElement(tagName, attributes, children) with three optional arguments.

Has shortcut tag().

Sample usage

Empty DIV:

// Creates <div></div>

Empty SPAN:

// <span></span>

Empty DIV with ID attribute:

createElement('div', {id: 'test'})
// <div id="test"></div>

Element DIV with text content:

createElement('div', null, 'Inner text')
// Creates <div>Inner text</div>

Element DIV with children and CLASS attribute:

    {classList: ['some', {'class': false, 'name': true}]},
        createElement('div', {'class': 'some classes'}, 'First child'),
        'Text node',
        createElement('footer', null, 'Last child')
 * Creates according structure:
 * <div class="some name">
 *     <div class="some classes">First child</div>
 *     Text node
 *     <footer>Last child</footer>
 * </div>


String, default value 'DIV'

Defines the tag, that will be created.


Object, default value {}

This object should contain attributes that will be assigned to created element.

There are some edge-cases:


Set value string as class attribute.

Has shortcut _ to use instead of key 'class'.

createElement('div', {'class': 'demo'})
createElement('div', {_: 'demo'})
// returns the same: <div class="demo"></div>

The more flexible way to assign classes. Can be string, array, object or function.

If function passed, it will be called and the result will be used. The string value will be separated by spaces and acted as classList. In case of array, all non-falsie values will be used as classList. The object will be filtered by non-falsie values and keys that left will be used as classList.


Sets the inline styling.

Can be string like 'font-size: 2em' or object like {fontSize: '2em'}


Sets the data-attributes.

The attribute passed as {data: {id: 'test'}} will be assigned as data-id="test" attribute to generated element.

event listeners

All attributes that has function as value will be considered as event listeners and will be added to element as callbacks to according events.

let flag = 0
const subDOM = createElement('div', {click: () => flag++}) // same as user click on element
// flag == 1


Array, String, Function or Element, default value []

The list of element's children.

If String or Element passed, they will be acted as single element of array of children.

If element of array is Function, it will be called abd the result of it will be used as child.

Additional notes

If attributes passed as String, it will be considered as a className.

If attributes passed as Element, it will be considered as a first child.

Create Fragments

You can create element with empty (but not undefined) tag name and receive DOM fragment.

const fragment = createElement(null, null, [
    createElement('span', null, 'SPAN element'),
    'Text node'


 * This code generates under the BODY element and text nodes:
 * <span>SPAN element</span>
 * Text node

To simplify usage, fragment also contains innerHTML getter that returns fragment's content.

 * Output:
 * <span>SPAN element</span>Text node

As tag name can be any falsie value except undefined.

There are two shortcuts prepared to create fragments: createFragment(children[]) and the same fragment(children[])

Setting the innerHTML

It is possible to set the innerHTML of newly creating element with innerHTML helper function.

This function just converting the html string into DOM elements via proxy fragment.

createElement('div', null, innerHTML('<span>Here is html</span>'))
 * Output:
 * <div>
 *     <span>Here is html</span>
 * </div>

Add styles to the page

It is quite easy to add a styling to the page using the style tag:

createElement('style', null, 'pre {color: navy; background-color: lightgray}')
 * Output:
 * <style>pre {color: navy; background-color: lightgray}</style>

But you can also create the same element using css helper and pass there an object with selectors and CSS declarations:

const styles = {
    pre: {
        color: 'navy',
        backgroundColor: 'lightgray'

 * Output:
 * <style>pre {color: navy; background-color: lightgray}</style>


All HTML5 tags shortcuts are prepared in the tags.js file.

You can build the DOM using tag functions like div() instead of createElement('div'). These shortcuts were used in the examples above.

There is one additional shortcut: text(textContent). This function creates document fragment with text node inside it. You can use it like this:

span(text('Inner text'))
// <span>Inner text</span>

Package Sidebar


npm i dom-element-factory

Weekly Downloads






Unpacked Size

32 kB

Total Files


Last publish


  • liksu