Skip to content

Instantly share code, notes, and snippets.

@nltncsr
Created March 16, 2017 22:25
Show Gist options
  • Save nltncsr/d125d9aac3a5e88fbf4d9583025098f6 to your computer and use it in GitHub Desktop.
Save nltncsr/d125d9aac3a5e88fbf4d9583025098f6 to your computer and use it in GitHub Desktop.
A function that creates DOM elements given its tag name, classes and attributes
const newEl = function(tag, classes, attrs) {
let el = document.createElement(tag);
if(classes) {
if(typeof(classes) === 'string')
el.classList.add(classes)
else
classes.forEach((c) => el.classList.add(c))
};
if(attrs) {
for(attr in attrs) {
if(attrs.hasOwnProperty(attr))
el[attr] = attrs[attr]
};
};
return el;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment