Easy Tutorial
❮ Verilog2 Codestyle Android Tutorial Telephonymanager ❯

jQuery Check if ID or Element Exists

Category Programming Techniques

We can use the length property of jQuery to check if an id exists:

Example

if ($('#myElement').length > 0) { 
    // 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>');
});

** Click to Share Notes

Cancel

-

-

-

❮ Verilog2 Codestyle Android Tutorial Telephonymanager ❯