Skip to content

Instantly share code, notes, and snippets.

@phillbaker
Created May 9, 2013 17:03
Show Gist options
  • Save phillbaker/5548865 to your computer and use it in GitHub Desktop.
Save phillbaker/5548865 to your computer and use it in GitHub Desktop.
// jQuery Plugin ifExists
// v0.0.1
// @phillbaker
//
// To be used if you want to call a (non-core jquery) function on the result of a jquery selector.
//
// Example:
//
// html:
// <button id="start">Start</button>
//
// js:
// $("#start").ifExists(
// function(elem){
// elem.animate({marginTop:20},function(){alert('ok')}).addClass('aaa');
// }
// );
//
// http://jsfiddle.net/andres_314/vbNM3/2/
// http://stackoverflow.com/a/11768171
$.fn.ifExists = function(fn) {
if (this.length) {
$(fn(this));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment