Rudchyk
5/26/2017 - 1:53 PM

Check if Element Exists

Check if Element Exists

$.fn.exists = function(callback) {
  var args = [].slice.call(arguments, 1);

  if (this.length) callback.call(this, args);

  return this;
};

// Usage
$('#myElement').exists(function() {
  this.append('<p>I exist!</p>');
});
if($('#myElement').length) {
  // it exists
}