When an element is removed from the DOM using removeChild()
, the reference to the element still exist but the element is no more in the DOM.
How to know if an HTML element (or its parents) is still attached to a document ?
The HTML DOM is an Object Model for HTML. It defines: HTML elements as objects. Properties for all HTML elements. Methods for all HTML elements.
The document object is the root node of the HTML document. The document object is a property of the window object. The document object is accessed with: window.document or just document.
The <title> HTML element defines the document's title that is shown in a browser's title bar or a page's tab. It only contains text; tags within the element are ignored. Metadata content.
Node.prototype.contains()
is the answer:
if(document.body.contains(yourElement)) {
// Yep, it's attached.
}
Compatibility: IE5+
For newer browsers (no IE support), you can use Node.isConnected
, which is a property on Node
and returns a boolean.
Mozilla Node.isConnected
document.querySelectorAll('#myElement').isConnected;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With