Skip to content

Instantly share code, notes, and snippets.

@uzulla
Created January 24, 2012 20:10
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 uzulla/1672273 to your computer and use it in GitHub Desktop.
Save uzulla/1672273 to your computer and use it in GitHub Desktop.
JQuery add-on that add an If statement
//JQuery add-on that add an If statement
$.fn.ift = function(){
var flag = arguments[0];
var func = arguments[1];
var else_func = arguments[2];
if(flag && $.isFunction(func)){
func.call(this, this);
}else if(!flag && $.isFunction(else_func)){
else_func.call(this, this);
}
return this;
}
/*
example.
$('body')
.ift(true, function(){$(this).css('background-color','blue')} )
.ift((1!=1), function(){$(this).css('background-color','gray')} )
.ift(
some.isValid(),
function(){$(this).css('background-color','green')},
function(){$(this).css('background-color','red')}
);
also, coffeescript.
$('<span />')
.ift(
src.pin==1
()-> $(this).addClass('check').text('!') # then
()-> $(this).addClass('nocheck').text('-') #else
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment