-
-
Save nullhook/d981700fd07663f12341de358c8621f1 to your computer and use it in GitHub Desktop.
tiny helper to create 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
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