client/src/dom.js

37 lines
762 B
JavaScript
Raw Normal View History

2018-11-17 19:07:58 +01:00
function Dom() {
return {
clear: clear,
make: make
}
function clear(elem) {
while(elem.firstChild) {
elem.removeChild(elem.firstChild);
}
}
function make(tag, properties, children) {
var e = document.createElement(tag);
properties = properties || {};
children = children || [];
for(key in properties) {
var value = properties[key];
switch(key) {
case "class":
e.className = Array.isArray(value) ? value.join(' ') : value;
break;;
case "onClick":
e.addEventListener("click", value);
break;;
default:
e[key] = value;
}
}
for(var i = 0; i < children.length; i++) {
e.appendChild(children[i]);
}
return e;
}
}