Created
July 12, 2022 13:12
-
-
Save joaotanaca/6d59ec35f53e8d71c4fdd08c47f7266e to your computer and use it in GitHub Desktop.
Função para criação de elementos com envio de propriedades no momento da criação
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
/** | |
* | |
* @template {keyof HTMLElementTagNameMap} K | |
* @param {K} elementTag | |
* @param {HTMLElementTagNameMap[K]} options | |
* @returns {HTMLElementTagNameMap[K]} | |
*/ | |
export function createElement(elementTag, options = {}) { | |
const element = document.createElement(elementTag); | |
Object.entries(options).forEach(([key, value]) => { | |
if (typeof element[key] === "function" && key.slice(0, 1) !== "on") { | |
element[key](value); | |
} else { | |
element[key] = value; | |
} | |
}); | |
return element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment