Skip to content

Instantly share code, notes, and snippets.

@pniaps
Created December 29, 2015 08:20
Show Gist options
  • Save pniaps/b01d8ec4f3d86ee8556c to your computer and use it in GitHub Desktop.
Save pniaps/b01d8ec4f3d86ee8556c to your computer and use it in GitHub Desktop.
jQuery run code if element exists
/**
* Tiny jQuery Plugin
* by Chris Goodchild
* https://css-tricks.com/snippets/jquery/check-if-element-exists/
*
*/
$.fn.exists = function(callback) {
if (this.length) {
var args = [].slice.call(arguments, 1);
callback.call(this, args);
}
return this;
};
// Usage
$('div.test').exists(function() {
this.append('<p>I exist!</p>');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment