Created
March 16, 2017 22:25
-
-
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
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
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