Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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