-
Notifications
You must be signed in to change notification settings - Fork 2
/
dom-tools.js
53 lines (45 loc) · 1.35 KB
/
dom-tools.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
52
53
export function inputInt(id) {
return parseInt(document.getElementById(id).value, 10);
}
export function addEl(parent, tag) {
const el = document.createElement(tag);
parent.appendChild(el);
return el;
}
export function setText(el, text) {
el.innerHTML = "";
el.appendChild(document.createTextNode(text));
}
export function defaultText(el, text) {
if (!el.innerText) setText(el, text);
}
export function addInput(parent, id, label, value, onChange) {
const labelEl = addEl(parent, 'label');
setText(labelEl, label);
const input = addEl(labelEl, 'input');
input.id = id;
input.value = value;
input.addEventListener('change', onChange);
}
export function addTextArea(parent, id, label, value, onChange) {
const labelEl = addEl(parent, 'label');
setText(labelEl, label);
const input = addEl(labelEl, 'textarea');
input.id = id;
input.value = value;
input.addEventListener('change', onChange);
}
export function classIf(el, className, condition) {
if (condition) el.classList.add(className);
else el.classList.remove(className);
}
export function clearClass(className) {
for (const el of [...document.getElementsByClassName(className)])
el.classList.remove(className);
}
export function addButton(parent, label, callback) {
const button = addEl(parent, 'button');
setText(button, label);
button.addEventListener('click', callback);
return button;
}