Create complex DOM elements/trees using a functional approach

hyperscript, dom, element, node, create dom, morphdom, nanomorph, unidirectional, virtual-dom
npm install elementx@3.1.2



⚡️​ Create complex DOM elements/trees using a functional approach.

npm version build status test coverage dependency status license js standard style downloads per month

This module provides an alternative to JSX or template strings for those who want to build up their DOM trees using plain function composition.

const { div, h1, h2, button, ul, li } = require('elementx')

  h1('.bold', 'elementx'),
  h2('#subtitle', 'Create a DOM tree with ease'),
  button({ href: '' }, 'Open'),
    ['simple', 'functional', 'fast']
      .map(key => li(key))


  • Universal - Works in Node and Browser
  • SVG Support - Supports creating SVG Elements
  • Functional - Since it's just function composition we can arbitrarily compose them
  • Small Only 1.99 kB minified and gzipped
  • Interoperability Can be used with diffing libraries like morphdom, nanomorph or anyhting that uses the DOM


> npm install elementx


const { div, h1, a } = require('elementx')

const node = div(
  h1({ class: 'title' }, 'This is a title'),
  div({ class: 'bg-red' },
    a({ href: '' }, 'Github')

// mount the tree to the DOM

 * ->
 * <div class="title">
 *   <h1>This is a title</h1>
 *   <div class="bg-red">
 *     <a href="">Github</a>
 *   </div>
 * </div>

Getting Started

Each HTML tag is exposed as a function when requiring elementx.

// using destructuring
const { div, h1, p, button } = require('elementx')

These functions have the following syntax:

tag(selector, attributes, children)

All arguments are optional with at least one argument needing to be present. This kind of function overloading allows you to iterate on your DOM structure really fast and reduce visual noise.

  • selector can be .title to append a class or #id to give the element an id. These can be mixed as you might expect:
  • attributes is an object of dom attributes: { href: '#header' }
  • children can be a string for a text node or an array of nodes

Lifecycle hooks

This module aims to be just the element creation layer. It can be used with any view framework using DOM as their base element abstraction for diffing. Some libraries like this include choo or inu.


SVG works as expected. Sets the appropriate namespace for both elements and attributes. All SVG tags can only be created with the h-helper:

const { svg } = require('elementx')

const node = svg({
  viewBox: '0 0 0 32 32',
  fill: 'currentColor',
  height: '32px',
  width: '32px'
}, [
  h('use', { 'xlink:href': '#my-id' })


Use without helper functions

Sometimes you need to fall back to the traditional createElement(tag, attributes, children) (aliased to h), for example svg tags.

const h = require('elementx')

const node = h('h1', 'text')

 * ->
 * <h1>text</h1>


All HTML DOM Events can be attached. The casing of the event name doesn't matter (onClick, onclick, ONCLICK etc.)

const node = h.button({
  onClick: () => console.log('button has been clicked')


External tools


> npm test