gavsiu gavsiu - 1 year ago 95
jQuery Question

JQuery: Check if element exists alongside '.remove()'

I know you can check if an element exists with

, but when an element is destroyed using
still reports the div exists. How can I find whether or not it actually exists?

if ($('div').length) {
} else {

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download