forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.js
More file actions
27 lines (25 loc) · 744 Bytes
/
Util.js
File metadata and controls
27 lines (25 loc) · 744 Bytes
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
'use strict';
{
class Util {
/**
* Creates an element, optionally setting its attributes, and appends
* the element to a parent.
* @param {string} name The tag name of the element to create.
* @param {HTMLElement} parent The parent element.
* @param {Object} options An object with attribute names and values.
*/
static createAndAppend(name, parent, options = {}) {
const elem = document.createElement(name);
parent.appendChild(elem);
Object.entries(options).forEach(([key, value]) => {
if (key === 'text') {
elem.textContent = value;
} else {
elem.setAttribute(key, value);
}
});
return elem;
}
}
window.Util = Util;
}