Skip to content

Instantly share code, notes, and snippets.

@ajace
Created August 30, 2013 18:20
Show Gist options
  • Save ajace/6392806 to your computer and use it in GitHub Desktop.
Save ajace/6392806 to your computer and use it in GitHub Desktop.
function dom(name, attributes) {
var node = document.createElement(name);
if (attributes) {
forEachIn(attributes, function(name, value) {
setNodeAttribute(node, name, value);
});
}
for (var i = 2; i < arguments.length; i++) {
var child = arguments[i];
if (typeof child == "string")
child = document.createTextNode(child);
node.appendChild(child);
}
return node;
}
function setNodeAttribute(node, attribute, value) {
if (attribute == "class")
node.className = value;
else if (attribute == "checked")
node.defaultChecked = value;
else if (attribute == "for")
node.htmlFor = value;
else if (attribute == "style")
node.style.cssText = value;
else
node.setAttribute(attribute, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment