dom-create-element
v1.0.2
Published
Create DOM elements.
Downloads
427
Readme
dom-create-element
Create DOM elements.
Usage
npm install dom-create-element
var create = require('dom-create-element');
var el = create({
selector: 'div',
id: 'el-id',
styles: 'el-classes',
children: create({
selector: 'a',
link: 'http://google.com',
target: '_blank',
html: 'Link'
})
});
document.body.appendChild(el);
Available options:
selector
: the string type tag of the HTML element (required)id
: unique ID selector of the elementstyles
: CSS classes of the element (this is not inline styles)attr
: custom attributeslink
: ifselector
is a<a>
element, provide ahref
(required)target
: ifselector
is a<a>
element, you can define targetsrc
: ifselector
is an<img>
element, provide asrc
lazyload
: ifselector
is an<img>
element, you can definelazyload
(see below)html
: the content of the element (basically innerHTML)children
: the children of the element. could be another instance ofcreate
so you could chain element creation
For images, you can pass lazyload: true
.
This will set the image to opacity: 0
, then go back to 1
when it's loaded.
var image = create({
selector: 'img',
src: '/path/to/img.jpg',
lazyload: true
});
TODOs
- [x] lazyload for images
- [ ] cleaner options:
opt
should first be the selector with more options as second parameter - [ ] tests
License
MIT, see LICENSE.md for details.