Skip to content

Instantly share code, notes, and snippets.

@itsalb3rt
Last active May 21, 2019 15:57
Show Gist options
  • Save itsalb3rt/2eb160656ee5c247b3e51bff0711f40f to your computer and use it in GitHub Desktop.
Save itsalb3rt/2eb160656ee5c247b3e51bff0711f40f to your computer and use it in GitHub Desktop.
Crear elementos del doom utilizando javascript
function createElement(tagName, id = null, __class = null, src = null,customAtribute = null) {
let element = document.createElement(tagName);
element.setAttribute('id', (id == null) ? '' : id);
if (Array.isArray(__class)) {
__class.forEach(item => {
element.classList.add(item);
});
} else {
element.classList.add((__class == null) ? 'none' : __class);
}
if (tagName == 'img') {
element.src = src;
}
if(Array.isArray(customAtribute)){
customAtribute.forEach(atributes=>{
for(let key in atributes){
element.setAttribute(key,atributes[key])
}
})
}
return element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment