Tutorial Check if Element Exists
if ($('#myElement').length > 0) { // it exists } Or to make it a fancy function with a callback: // Tiny jQuery Plugin // by ...
https://iskablogs.blogspot.com/2014/03/tutorial-check-if-element-exists.html
if ($('#myElement').length > 0) {
// it exists
}
Or to make it a fancy function with a callback:
// Tiny jQuery Plugin
// by Chris Goodchild
$.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('
});
// it exists
}
Or to make it a fancy function with a callback:
// Tiny jQuery Plugin
// by Chris Goodchild
$.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!
');});