Martin Labuschin
Martin Labuschin

Reputation: 516

jQuery: Check if a element exists (which could possibly be added through ajax)

I already know how to check elements that are there when the document is ready:

jQuery.fn.exists = function () { 
  return jQuery(this).length > 0; 
}

But this method doesn’t know elements that are added with AJAX. Does anybody know how to do that?

Upvotes: 1

Views: 2480

Answers (1)

Eivind
Eivind

Reputation: 841

The method does once the ajax is loaded and appended to the DOM. You could rewrite it a bit:

jQuery.existsin = function (what, where) { 
  return jQuery(where).find(what).length > 0; 
}

The you could on ajax success :

function(data, status){
  if(jQuery.existsin('selector', data)){
    //do foo
  }
}

Upvotes: 3

Related Questions