Skip to content

Instantly share code, notes, and snippets.

@ghoullier
Created April 8, 2016 09:54
Show Gist options
  • Save ghoullier/f5a7a5909ff295e44626df3e7c81f202 to your computer and use it in GitHub Desktop.
Save ghoullier/f5a7a5909ff295e44626df3e7c81f202 to your computer and use it in GitHub Desktop.
DOM - createElement Helper
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