jQuery.exists
A really simple jQuery extension that will check if a DOM selector actually is present in the dom and then run a callback function. Supports jQuery chaining as well.
https://gist.github.com/4148429
// Calls function on $ if it is an existing piece of dom.
// $('foo.bar').exists(function(){$("#target").show();}) -> foo.bar will appear where foo.bar is present
// $('foo.baz').exists(function(){$("#target").show();}) -> foo.baz will not appear where foo.baz is not present
jQuery.fn.exists = function(func){if (this[0]) {if (func){func.apply(this, arguments);} else {return true;}} else {if (func){return this;} else {return false;}};};
Written by Avi Flombaum
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Related Tags
#jquery
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#