dom-gen v2.3.0
Utility for dom generation, a jquery plugin
Idea
This tool is a shorthand of $('<tagName/>', opts)
. You can write it like tagName(opts)
with this tool such as div({data: {key: 'value'}})
for example.
See the slide "Things You Might Not Know About jQuery" by John Resig. This slide explains how $('<tagName/>', opts)
works in details.
Install
npm install dom-gen
Usage
NOTE dom-gen supposes $ is exposed in global namespace. You need to put jquery on global namespace first.
div()
// This creates an empty div element.
The above code is the same as $('<div/>')
. You can chain jquery method calls like the following
text'Hello'
div(opts)
You can pass generation options as the parameter.
The above is the same as:
(See the slide "Things You Might Not Know About jQuery" which explains what the above code means in jQuery.)
or:
data x: 0 y: 1
Another example
is the same as:
div(opts, param0, [param1, ...])
You can pass additional params to div
function and they are append
ed to the element.
is the same as the follwoing jquery call:
which is equivalent of the following html:
<div class="main"><div>Hello</div>world!</div>
You can even omit first param opts
if it's empty.
is equal to:
<div><p><span>Hello</span> <span>world!</span></p></div>
Supported tags
dom-gen
exports following tags by default for now. You can import them directly from dom-gen
and use them as generator functions.
var
Create your own
You can create the generator for your own tag.
const xTag = // This is equivalent of <x-tag class="foo"><p>Hello</p></x-tag>
Recipes
Mix inline html and dom-gen composition
Inline elements are often better not to use dom-gen for creating.
is a bit better readable than:
p(
'Hello, this is ',
span({addClass: 'green'}, 'example'),
'page!'
)
Complex construction
is equivalent of the following html:
Hello Hello, this is example page!
Use with tagged template string
License
MIT
Similar projects
- Ruby
- Markaby Ruby
- erector Ruby
- Builder Ruby
- Nokogiri HTML Builder Ruby
- JavaScript
- @cycle/dom JavaScript
- hyperscript-helpers JavaScript
- jm JavaScript
- hyperscript JavaScript
- transform-react-jsx JavaScript
- Elem JavaScript
- frzr JavaScript
- CoffeeScript