Created
May 29, 2014 22:01
-
-
Save markwatson/427f712866b7e8266692 to your computer and use it in GitHub Desktop.
Create elements in javascript. Useful to me, not that useful in general.
This file contains hidden or 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
var createEl = function(type, props, children) { | |
var el = document.createElement(type); | |
if (props) { | |
for (var key in props) { | |
if (props.hasOwnProperty(key)) { | |
if (key == "css") { | |
if (props[key].length > 0) { | |
el.style.cssText = props[key].join(';') + ";"; | |
} | |
} else { | |
el.setAttribute(key, props[key]); | |
} | |
} | |
} | |
} | |
if (children) { | |
for (var i = 0;i < children.length;i++) { | |
el.appendChild(children[i]); | |
} | |
} | |
return el; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment