Skip to content

Instantly share code, notes, and snippets.

@7pulse
Created October 31, 2022 16:24
Show Gist options
  • Save 7pulse/372110e0bf46745c595ab1b497256496 to your computer and use it in GitHub Desktop.
Save 7pulse/372110e0bf46745c595ab1b497256496 to your computer and use it in GitHub Desktop.
function for creating HTML elements dynamically
function e(type, attributes, ...children) {
const el = document.createElement(type);
Object.entries(attributes).forEach(entry => {
el.setAttribute(entry[0], entry[1]);
});
children.forEach(child => {
if (typeof child ==='string') {
el.appendChild(document.createTextNode(child));
} else {
el.appendChild(child);
}
});
return el;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment