Skip to content

Instantly share code, notes, and snippets.

@batazo
Created August 10, 2021 19:35
Show Gist options
  • Select an option

  • Save batazo/f3e2d55e19123788b291752d760c8162 to your computer and use it in GitHub Desktop.

Select an option

Save batazo/f3e2d55e19123788b291752d760c8162 to your computer and use it in GitHub Desktop.
DOM ELEMENT FACTORY
const elFactory = (type, attributes, ...children) => {
const el = document.createElement(type);
for (key in attributes) {
el.setAttribute(key, attributes[key]);
}
children.forEach((child) => {
if (typeof child === "string") {
el.appendChild(document.createTextNode(child));
} else {
el.appendChild(child);
}
});
return el;
};
const redDivCreator = elFactory(
"div",
{
id: "reddiv",
class: "dobozok",
style: "background-color: red; color: white; padding: 5px 5px; border: solid 5px black;"
},
elFactory("span", {}, "EZ A"),
" PIROS DIV !"
);
const greenDivCreator = elFactory(
"div",
{
id: "greendiv",
class: "dobozok",
style: "background-color: green; margin: 5px 5px; color: white; padding: 5px 5px; border: solid 5px black;"
},
elFactory("span", {}, "EZ A"),
" ZÖLD DIV !"
);
document.body.appendChild(redDivCreator);
reddiv.appendChild(greenDivCreator);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment