Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if cached jquery object is still in DOM

Does anyone know how to tell if a cached jQuery object has gone stale, e.g. is no longer in the DOM? For example:

var $cached_elem = $('.the_button');

// .. and then later

$cached_elem.text('updating...');

I have recently encountered the situation where the $cached_elem is removed from the DOM due to some other event. So what I would like to do:

if ( $cache_elem.isStillInDOM() ){
  // now do time consuming stuff with $cached_elem in DOM 
}

Before anyone offers, I have already employed this, which is a fair analog for what I'm trying to do:

if ( $cached_elem.is(':visible') === true ){ ... }

However, this is not really the same thing and could fail in some cases.

So can anyone think of a simple way to check directly if a cached jQuery object is "stale"? I may be forced to write a plugin if not ...

like image 588
Michael Mikowski Avatar asked Oct 28 '10 07:10

Michael Mikowski


2 Answers

if($elem.closest('body').length > 0) seems like it could do the trick.

$(function() {
    var $button = $(".the_button");
    alert (isStale($button));
    $button.remove();
    alert (isStale($button));
});
    
function isStale($elem)
{
    return $elem.closest("body").length > 0;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <span class="the_button">Hello World</span>
</div>

Edit: Updated in response to Yi Jiang's comment so that it will return correctly if its parent element is removed

Edit 2: Updated in response to lonesomeday's comment - changed parents() to 'closest()` for performance improvement

like image 193
Steve Greatrex Avatar answered Oct 05 '22 11:10

Steve Greatrex


The native document.contains() method should be faster than jQuery to determine if a cached jQuery element exists in the DOM.

if (document.contains($cached_elem[0])) {
    // Element is still in the DOM
}
like image 33
Thomas Higginbotham Avatar answered Oct 05 '22 13:10

Thomas Higginbotham