Created
June 26, 2016 20:18
-
-
Save jonathanwork/cdf8878f6e0ff06dfd7bdf50cdfae892 to your computer and use it in GitHub Desktop.
this a simple way of creating element in js
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
//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