Last active
May 17, 2020 20:10
-
-
Save stiletto/90c9826f1c50550fdccdb67105078631 to your computer and use it in GitHub Desktop.
Simple helper for creating DOM elements
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
/* Simple helper for creating DOM elements. Released into public domain. | |
* https://gist.github.com/stiletto/90c9826f1c50550fdccdb67105078631 | |
*/ | |
function make(tag, props, children) { | |
var element = document.createElement(tag); | |
if (props) for(let key of Object.keys(props)) { | |
let value = props[key]; | |
if (key=="style" && typeof(value)=="object") // you may throw out this branch if you don't care about inline styles | |
for(let style of Object.keys(value)) element.style[style] = value[style]; | |
else | |
element[key] = value; | |
} | |
if (children) for(let child of children) { | |
if (typeof(child) == "string") | |
element.appendChild(document.createTextNode(child)); | |
else | |
element.appendChild(child); | |
} | |
return element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment