Skip to content

Instantly share code, notes, and snippets.

@ImOnALampshade
Last active July 29, 2023 03:26
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 ImOnALampshade/ceb56692d0edfed57daa23578dc69994 to your computer and use it in GitHub Desktop.
Save ImOnALampshade/ceb56692d0edfed57daa23578dc69994 to your computer and use it in GitHub Desktop.
JSX function for creating DOM elements
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;
}
@ImOnALampshade
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment