lets say you set var $this = jQuery(this); how can you search the dom for that element. $this.size() Does not work because it’s not searching the dom for that element. I need to basically detect if $this has been removed from the dom and stop an interval.
Update code requested:
jQuery(document).find('div.ctalk').each(function(){
var delay;
var $this = this;
activateLive = function($this) {
clearInterval(delay);
delay = setInterval(function(){
if(jQuery($this).size() != '0') {
fn.actJSON('0', $this, 'update=1&');
}else {
alert('kill it');
clearInterval(delay);
}
}, 10000 );
}
if(!jQuery(this).data('init')) {
jQuery(this).data('init', '1');
fn.activateBinds($this);
activateLive($this);
}
});
This would probably be the first time I’ve advocated the use of
.is()for anything, but it does make this quite a simple task:You can swap
"body"for"html"if you want to check nodes anywhere in the document.IE also has a proprietary
sourceIndexproperty that would do the job with slightly better performance:Extra Update: I knew there was a couple of other methods for this, they were scratching at the back of my mind but I couldn’t quite remember. Modern browsers have the DOM Level 3 method
.compareDocumentPosition(), so you can do the whole thing without jQuery if anyone ever needs to.The other method I was thinking of is IE’s
.contains()method, which also works. However, using that would require a check that the function is defined first so it’s easier to just stick withsourceIndex.