Skip to content

Instantly share code, notes, and snippets.

@jonathanwork
Created June 26, 2016 20:18
Show Gist options
  • Save jonathanwork/cdf8878f6e0ff06dfd7bdf50cdfae892 to your computer and use it in GitHub Desktop.
Save jonathanwork/cdf8878f6e0ff06dfd7bdf50cdfae892 to your computer and use it in GitHub Desktop.
this a simple way of creating element in js
//appending elements in js....
function createElements(element, attr, inner) {
//checkes whether the user has logged in an element
if(typeof(element) === 'undefined') return false;
//checks whether the user has logged in an inner link
if(typeof(inner) === 'undefined') return inner = 'undefined';
//if ok the element is created
var el = document.createElement(element);
//the attr is obj then we get the keys and set them up
if(typeof(attr) === 'object') {
for(var key in attr) el.setAttribute(key, attr[key]);
}
if(!Array.isArray(inner)){
inner = [inner];
}
for(var k = 0; k < inner.length; k++) {
if(inner[k].tagName) {
el.appendChild(inner[k]);
} else {
el.appendChild(document.createTextNode(inner[k]));
}
}
return el;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment