Skip to content

Instantly share code, notes, and snippets.

@raisiqueira
Created April 25, 2020 15:43
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 raisiqueira/e978259cf1e89e5f3432904960580942 to your computer and use it in GitHub Desktop.
Save raisiqueira/e978259cf1e89e5f3432904960580942 to your computer and use it in GitHub Desktop.
Basic implementation for create dynamic HTML Elements
function createElement<T extends keyof HTMLElementTagNameMap>(
tag: T,
attrs = {},
args: string | typeof createElement
): HTMLElement {
const element = document.createElement(tag);
for (const attr in attrs) {
element[attr] = attrs[attr];
}
Array.from(arguments).slice(2).forEach(
n => element.appendChild(
typeof n === 'string' ? this.document.createTextNode(n || '') : n));
return element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment