lightweight dom builder
npm install jdom
intended usage with webpack or browserify
const {
createElement, updateElement,
DIV, SPAN, SCRIPT, STYLE, // ...etc
$, $$,
on, once, off, dispatch,
currentScript,
style,
isObject, isArray, isElement
} from 'jdom';
createElement (string tag, object props, string ns);
create dom/svg elements
const container = createElement('div', {
id: 'a',
style: {
color: 'red'
},
click: () => {
this.style.color = 'blue';
}
})
updateElement (HTMLElement elem, object props);
update dom elements
const element = document.getElementById('asdf');
const container = updateElement(element, {
style: {
color: 'blue'
}
})
DOM FACTORY METHODS
syntastic sugar
import {DIV, SCRIPT, SPAN} from 'jdom';
const div = DIV({
id: 'myDiv',
children: [
'injecting script',
SCRIPT({src: 'http://some.url'}),
'done'
],
parent: document.body
});
SPAN({parent: div, children: ['!!!!']);
SVG FACTORY METHODS
syntastic sugar
import {SVG, RECT, CIRCLE} from 'jdom';
SVG({
id: 'mySVG',
width: 200,
height: 200,
viewBox: '0 0 200 200',
children: [
RECT({
fill : 'red',
x:5,
y:5,
width: 190,
height: 190
}),
CIRCLE({
fill: 'yellow',
cx: 100,
cy:100,
r:80
})
],
parent: document.body
});
const b = $('#b');
const c = $$('.c');
- on (HTMLElement elem, string event, function handler)
- once (HTMLElement elem, string event, function handler)
- off (HTMLElement elem, string event, function handler)
- dispatch (HTMLElement elem, string event);
const someDiv = document.getElementById('someDiv');
on(someDiv, 'mouseover', () => {
someDiv.style.backgroundColor = 'red';
});
once(someDiv, 'click', () => {
someDiv.parentNode.removeChild(someDiv);
});
off(someDiv, 'click', someFunction);
dispatch(someDiv, 'click');
currentScript ()
get the currently executing script
const thisScript = currentScript();
console.log(thisScript.src);
style (HTMLElement elem, object props)
update element style
const someDiv = document.getElementById('someDiv');
style(someDiv, {
color: 'green',
backgroundColor: 'red'
})
- isObject (object obj)
- isArray (object obj)
- isElement (object obj)
- toCamelCase (string str)