Last active
May 18, 2017 16:59
-
-
Save lizardking8610/655b3c4f44eae4a93399e101cec0ae34 to your computer and use it in GitHub Desktop.
jQuery: does element exists
This file contains hidden or 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
if ( $('#myElement').length ) { | |
// it exists | |
} | |
//>0 was not required | |
//if ($('#myElement').length > 0) { | |
// it exists | |
}// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or with a function from css-tricks.com: `$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;
};
// Usage
$('div.test').exists(function() {
this.append('
I exist!
');});`