super simple dom template function
Last active
August 29, 2015 14:26
-
-
Save disjukr/90abba4966d79d2942f7 to your computer and use it in GitHub Desktop.
super simple dom template function
This file contains hidden or 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
function el(qs, ...children) { | |
let m = qs[0].match(/([^#\.]+)?(?:#([^\.]+))?(?:\.(.+))?/); | |
let el = document.createElement(m[1] || 'div'); | |
let id = m[2]; if (id) el.id = id; | |
let cls = m[3]; if (cls) el.className = cls.replace(/\./g, ' '); | |
for (let child of children) { | |
el.appendChild( | |
typeof child === 'string' ? | |
document.createTextNode(child) : | |
child | |
); | |
} | |
return el; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment