Created
August 10, 2021 19:35
-
-
Save batazo/f3e2d55e19123788b291752d760c8162 to your computer and use it in GitHub Desktop.
DOM ELEMENT FACTORY
This file contains hidden or 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
| 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