Given a jQuery object, $j
, I need to know if it represents something that is already on the page or if it is something that has not yet been put on the page. Fo
I'd probably just check to see if the element can reach the body element:
$j.closest('body').size()>0
As you noted in other comments, this would fail in some exceptional cases where you are handling extra body elements. If you really want to detect those situations, I guess you could check to see if you have multiple $(body
) elements but I suspect that such extreme cases will get out of control pretty quickly.
This "is-probably-attached-to-page" check could be turned in to a bona-fide jQuery selector, too:
$.expr[':'].isProbablyAttached = function(obj){
return $(obj).closest('body').size()>0;
};
// Usage:
$('.someClasses:isProbablyAttached').doSomething();