14

I know you can check if an element exists with $('div').length, but when an element is destroyed using .remove(), .length still reports the div exists. How can I find whether or not it actually exists?

if ($('div').length) { 
  alert('yes') 
} else { 
  alert('no') 
}
Flip
  • 6,233
  • 7
  • 46
  • 75
gavsiu
  • 757
  • 5
  • 9
  • 27
  • Err.. Not exactly sure what you mean. If you use `.remove()`, the element is removed from the DOM. Length reports the expected result: http://jsfiddle.net/TmPBC/ – Andrew Whitaker Jul 15 '11 at 23:33
  • That will alert yes as long as there is a div on the page. You can use an id to test if an exact element has been removed. – Paul Jul 15 '11 at 23:36
  • $('div') was an example. I was not testing to see if there were any divs in the entire page. – gavsiu Jul 15 '11 at 23:47
  • For the more generic question of "how to check if an element or one of its parents was removed", see [How do I check whether a jQuery element is in the DOM?](http://stackoverflow.com/q/3086068/323407) – Tgr Jun 27 '14 at 21:40

4 Answers4

9

By exists, you mean you want to see if it exists in the dom? Check to see if "html" is an ancestor:

var $myDiv = $(".myDiv");
$myDiv.closest("html").length;  // returns 1
$myDiv.remove();
$myDiv.closest("html").length;  // returns 0

Or use .is("html *"). It returns a boolean, which is handy:

var $myDiv = $(".myDiv");
$myDiv.is("html *"); // returns true
$myDiv.remove();
$myDiv.is("html *"); // returns false
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • If there is a difference at all, `.closest()` would be most efficient. Depending on how `.is()` is implemented, it could be almost as efficient as `.closest()`, or it could be much worse. – gilly3 Apr 02 '13 at 01:26
7

Test whether it has a parent:

if ($element.parent().length) { alert('yes') }
else { alert('no') }

or if you have a reference to the DOM element:

if(element.parentNode) {
    // yes
}

Obviously, this only works for elements you already have a reference to.

FWIW, the element itself still exists, it is just not part of the DOM tree.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • This won't work if any parent element of `$element` has been removed from the DOM. Eg, `$element.parent().remove()`. `$element.parent().length` is still 1, even though `$element` is not in the DOM tree. – gilly3 Jul 15 '11 at 23:55
  • @gilly3: Well, I assume that `.remove()` was performed on the element, as mentioned in the question. Anyway, +1 of your solution, but I think it is sufficient to look for `body` instead of `html`. – Felix Kling Jul 16 '11 at 00:05
4
if (!$foo.closest('html').length) {
    //Element is detached
}

This will still work if one of the element's parents was removed (in which case the element itself will still have a parent).

I cite this answer.

Community
  • 1
  • 1
user1444978
  • 124
  • 7
0

you can get the parent of element before removing the element and after the element has been removed you can check like this!

    var parent = $(element).parent();
    $(element).remove();

    if(parent.children(element)) {  alert('yes'); }
    else { alert('no'); }

of course element would be some jquery selector

Ehtesham
  • 2,967
  • 1
  • 18
  • 20