Created
September 26, 2011 16:07
-
-
Save thom-nic/1242597 to your computer and use it in GitHub Desktop.
Node builder (DOM) for Javascript
This file contains 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
/** | |
* Node builder. Call like: | |
* $n('div',{class:'top'},'inner text') // or: | |
* $n('div',{class:'top'},[$n('p',{},'nested element']) | |
*/ | |
$n= function(e,attrs,inner) { | |
if(typeof(e)=='string') e = document.createElement(e); | |
if (attrs) for (var k in attrs) e.setAttribute(k,attrs[k]); | |
if (inner) { | |
if (typeof(inner)=='string') e.textContent = inner; | |
else if (inner.call) inner.call(e); | |
else for (var i in inner) e.appendChild(inner[i]); | |
} | |
return e; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment