Created
August 11, 2022 22:51
-
-
Save skiano/cd2aa42594d4054d90aed0e37d81f452 to your computer and use it in GitHub Desktop.
A helper for creating dom stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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