Skip to content

Instantly share code, notes, and snippets.

@stiletto
Last active May 17, 2020 20:10
Show Gist options
  • Save stiletto/90c9826f1c50550fdccdb67105078631 to your computer and use it in GitHub Desktop.
Save stiletto/90c9826f1c50550fdccdb67105078631 to your computer and use it in GitHub Desktop.
Simple helper for creating DOM elements
/* 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