Created
December 29, 2015 08:20
-
-
Save pniaps/b01d8ec4f3d86ee8556c to your computer and use it in GitHub Desktop.
jQuery run code if element exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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