Skip to content

Instantly share code, notes, and snippets.

@lizardking8610
Last active May 18, 2017 16:59
Show Gist options
  • Save lizardking8610/655b3c4f44eae4a93399e101cec0ae34 to your computer and use it in GitHub Desktop.
Save lizardking8610/655b3c4f44eae4a93399e101cec0ae34 to your computer and use it in GitHub Desktop.
jQuery: does element exists
if ( $('#myElement').length ) {
// it exists
}
//>0 was not required
//if ($('#myElement').length > 0) {
// it exists
}//
@lizardking8610
Copy link
Author

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!

');
});`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment