Created
October 31, 2013 14:30
-
-
Save npup/7250766 to your computer and use it in GitHub Desktop.
Minimal element factory and DOM appender
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 E = (function () { | |
var global = this, doc = global.document; | |
function isArray(obj) {return "[object Array]"=={}.toString.call(obj);} | |
return { | |
"create": function (tag, attrs) { | |
var elem = doc.createElement(tag); | |
for (var attr in attrs) { | |
if ("html" == attr) {elem.innerHTML = attrs.html;} | |
else if ("class" == attr) {elem.className = attr;} | |
else {elem.setAttribute(attr, attrs[attr]);} | |
} | |
return elem; | |
} | |
, "append": function (elems, parent) { | |
isArray(elems) || (elems = [elems]); | |
parent || (parent = doc.body); | |
for (var idx=0, len=elems.length; idx<len; ++idx) {parent.appendChild(elems[idx]);} | |
return parent; | |
} | |
}; | |
})(); | |
E.append( | |
[ | |
E.create("h1", {"html": "Hey foo sister!!"}) | |
, E.create("p", {"html": "Buy more bar"}) | |
] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment