Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Created May 18, 2012 17:12
Show Gist options
  • Save brianswisher/2726477 to your computer and use it in GitHub Desktop.
Save brianswisher/2726477 to your computer and use it in GitHub Desktop.
Chain-able JavaScript with a Sprinkle of Syntactic Sugar, http://www.youtube.com/watch?v=6M2RWg_U6Gk&feature=g-upl
var util = (function(){
var id,
_target,
content,
css,
style,
format,
container,
html,
insert,
_element;
return {
id:function(i){id=i;_target=document.getElementById(id); return this;},
target:function(t){
var type = typeof(t)
if(type==='function') {
t.apply(this,[_target]);
} else {
_target = t;
}
return this;
},
content:function(c){_target.innerHTML=c; return this;},
css:function(c){css=c;for(var i in css){_target.style[i]=css[i];} return this;},
container:function(c){container=c; return this;},
html:function(h){html=h; return this;},
insert:function(i){
if(i!=null){insert=i};
_element = document.createElement(container);
_element.innerHTML = html;
switch (insert) {
case 'inside':
_target.appendChild(_element);
break;
case 'after':
_target.parentNode.insertBefore(_element,_target.nextSibling);
break;
}
_target = _element;
return this;
},
style:function(s){style=s; return this;},
format:function(f){format=f; _target.style[style]=format; return this;}
};
}());
util.id('lga').content('<div id="myStuff"></div>').id('myStuff').css({'font-size':'100px','font-family':'serif','text-align':'center'}).container('span').html('B').insert('inside').style('color').format('blue').html('o').insert('after').format('red').insert().format('orange').html('g').insert().format('blue').html('i').insert().format('green').html('e').insert().format('red').id('gbqfq').target(function(t){t.value='all night long'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment