Last active
July 29, 2023 03:26
-
-
Save ImOnALampshade/ceb56692d0edfed57daa23578dc69994 to your computer and use it in GitHub Desktop.
JSX function 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
function jsx(tag, attrs, ...children) { | |
let elem = document.createElement(tag); | |
for (const attr in attrs) { | |
const val = attrs[attr]; | |
elem.setAttribute(attr, val); | |
} | |
for (const child of children) { | |
if (child instanceof HTMLElement) { | |
elem.appendChild(child); | |
} else { | |
const txt = document.createTextNode(child); | |
elem.appendChild(txt); | |
} | |
} | |
return elem; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A quick function I wrote for turning JSX into DOM elements. In case you want to
use mostly vanilla JavaScript, but still want to be able to use JSX. Because
JavaScript frameworks are considered harmful.