Skip to content

Instantly share code, notes, and snippets.

@skiano
Created August 11, 2022 22:51
Show Gist options
  • Save skiano/cd2aa42594d4054d90aed0e37d81f452 to your computer and use it in GitHub Desktop.
Save skiano/cd2aa42594d4054d90aed0e37d81f452 to your computer and use it in GitHub Desktop.
A helper for creating dom stuff
function create(tag, attr = {}, children = []) {
children = Array.isArray(children) ? children : [children];
const elm = document.createElement(tag);
for (let a in attr) {
if (a.startsWith('on')) {
elm.addEventListener(a.slice(2).toLowerCase(), attr[a])
} else {
if (attr[a]) elm.setAttribute(a, attr[a]);
}
}
children.forEach((c) => {
if (!c) return;
if (typeof c === 'string') {
elm.innerText = c;
} else {
elm.appendChild(c);
}
});
return elm;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment