Skip to content

Instantly share code, notes, and snippets.

@markwatson
Created May 29, 2014 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwatson/427f712866b7e8266692 to your computer and use it in GitHub Desktop.
Save markwatson/427f712866b7e8266692 to your computer and use it in GitHub Desktop.
Create elements in javascript. Useful to me, not that useful in general.
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