Last active
July 4, 2023 17:21
-
-
Save nickytonline/8223b27b19c080c28d9f0d3b7fce1e82 to your computer and use it in GitHub Desktop.
A Handy DOM element creation function.
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
// Inspired from Sam Thorogood's article, https://dev.to/chromiumdev/beyond-appendchild-better-convenience-methods-for-html-55n4 | |
function createElement(nodeName, props) { | |
const { style = {}, ...propsNoStyle } = props; | |
const element = Object.assign(document.createElement(nodeName), propsNoStyle); | |
Object.entries(style).forEach(([key, value]) => { element.style[key] = value; }); | |
return element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the inspiration @samthor!