Skip to content

Instantly share code, notes, and snippets.

@nickytonline
Last active July 4, 2023 17:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickytonline/8223b27b19c080c28d9f0d3b7fce1e82 to your computer and use it in GitHub Desktop.
Save nickytonline/8223b27b19c080c28d9f0d3b7fce1e82 to your computer and use it in GitHub Desktop.
A Handy DOM element creation function.
// 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;
}
@nickytonline
Copy link
Author

Thanks for the inspiration @samthor!

@samthor
Copy link

samthor commented Jul 11, 2019

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment