Created
October 31, 2022 16:24
-
-
Save 7pulse/372110e0bf46745c595ab1b497256496 to your computer and use it in GitHub Desktop.
function for creating HTML elements dynamically
This file contains hidden or 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 e(type, attributes, ...children) { | |
const el = document.createElement(type); | |
Object.entries(attributes).forEach(entry => { | |
el.setAttribute(entry[0], entry[1]); | |
}); | |
children.forEach(child => { | |
if (typeof child ==='string') { | |
el.appendChild(document.createTextNode(child)); | |
} else { | |
el.appendChild(child); | |
} | |
}); | |
return el; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment