Created
April 8, 2016 09:54
-
-
Save ghoullier/f5a7a5909ff295e44626df3e7c81f202 to your computer and use it in GitHub Desktop.
DOM - createElement Helper
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
export const dom = (tag, attributes = {}, ...children) => { | |
const element = document.createElement(tag) | |
for (const attribute in attributes) { | |
if (attributes.hasOwnProperty(attribute)) { | |
element.setAttribute(attribute, attributes[attribute]) | |
} | |
} | |
const fragment = document.createDocumentFragment() | |
children.forEach((child) => { | |
if (typeof child === 'string') { | |
child = document.createTextNode(child) | |
} | |
fragment.appendChild(child) | |
}) | |
element.appendChild(fragment) | |
return element | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment