Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Created September 26, 2011 16:07
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 thom-nic/1242597 to your computer and use it in GitHub Desktop.
Save thom-nic/1242597 to your computer and use it in GitHub Desktop.
Node builder (DOM) for Javascript
/**
* 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