Skip to content

Instantly share code, notes, and snippets.

@nullhook
Last active June 30, 2020 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullhook/d981700fd07663f12341de358c8621f1 to your computer and use it in GitHub Desktop.
Save nullhook/d981700fd07663f12341de358c8621f1 to your computer and use it in GitHub Desktop.
tiny helper to create DOM elements
const $T = text => document.createTextNode(text)
function $E(tag, props, kids) {
const elem = document.createElement(tag)
for (const k in props) {
elem[k] = props[k]
}
for (const kid of kids) {
elem.appendChild(kid)
}
return elem
}
function applyStyle(selector, styles) {
const el = document.querySelector(selector);
if (el == null) return;
for (const prop in styles) {
el.style[prop] = styles[prop];
}
}
// Usage
const button = $E('button', {}, [
$E('i', {className: 'icon-lightbulb'}, []),
$T('I learned something!'),
$E('object', {data: '/confetti.svg', width: 30, height: 30}, []),
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment