/hyperscript-string

Create HTML strings with JavaScript

Primary LanguageJavaScript

haitch-html

Build status NPM version XO code style

Create HTML strings with JavaScript

Goes hand in hand with hyperscript-helpers.

Installation

Install haitch-html using npm:

npm install --save haitch-html

Usage

Module usage

const h = require('haitch-html');

const tag = h('ul.list', [
	h('li.item', 'Lorem ipsum'),
	h('li.item', {title: 'Second item'}, 'Dolor sit'),
	h('li.item', 'Amet')
]);
// an Haitch HTML tag object

const html = h.html(tag);
/*
<ul class="list">
	<li class="item">Lorem ipsum</li>
	<li title="Second item" class="item">Dolor sit</li>
	<li class="item">Amet</li>
</ul>
*/

Attributes that are null, undefined or false are excluded in the generated HTML.
Empty strings are left as is, e.g. h('a', {href: ''}) => '<a href=""></a>', and true are generated without value, e.g. h('button', {disabled: true}, 'Click me') => '<button disabled>Click me</button>'.
Void elements' children are excluded in the generated HTML as well.

Related packages

API

h(selector[, attrs, children])

Name Type Description
selector String Contains at least the HTML tag name
attrs Object Attributes for the HTML tag
children String or Array The children for the HTML tag

Returns: Object, the generated HTML.

License

MIT © Joakim Carlstein