We can usejQueryoflengthproperty to determineidwhether it exists:
It can also be written as a plugin, as follows, the plugin can determine whether an element exists.Example
$.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('<p>Exists!</p>');
});