-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
51 lines (43 loc) · 1.36 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// dom.js
// Martin Pravda
// implementation copied from James Diacono, dom.js
// The 'dom' function creates a new DOM element with the specified 'tag' name,
// or modifies an existing element.
// The 'properties' parameter is an object containing values to assign to the
// element's properties.
// The 'children' parameter is either a string or an array of elements.
// The 'properties' parameter may be omitted.
/*jslint browser */
function dom(tag, properties = {}, children = []) {
if (typeof properties === "string" || Array.isArray(properties)) {
children = properties;
properties = {};
}
if (!Array.isArray(children)) {
children = [children];
}
const element = (
typeof tag === "string"
? document.createElement(tag)
: tag
);
element.append(...children);
Object.keys(properties).forEach(function (name) {
element[name] = properties[name];
});
Object.assign(element.style, properties.style);
return element;
}
//demo import demo from "./demo.js";
//demo demo(
//demo dom("label", [
//demo "First name ",
//demo dom("input", {
//demo oninput(event) {
//demo console.log(event.target.value);
//demo },
//demo placeholder: "Grug"
//demo })
//demo ])
//demo );
export default Object.freeze(dom);