Javascript: how to tell if a node object has been inserted into a document/another element yet

Posted by thomasrutter on Stack Overflow See other posts from Stack Overflow or by thomasrutter
Published on 2010-04-12T05:56:18Z Indexed on 2010/04/18 6:43 UTC
Read the original article Hit count: 345

Filed under:
|
|

I'd like to be able to identify whether a given DOM node has been appended/inserted into another node yet, or whether it is fresh out of document.createElement() or similar and has not been placed anywhere.

In most browsers just checking the parentNode works.

if (!node.parentNode) {
  // this node is not part of a larger document
}

However, in Internet Explorer it appears that new elements, even right after they've been created with document.createElement() already have a parentNode object (of type DispHTMLDocument??).

Any other nice cross-browser and reliable way?

Edit: looks like Internet Explorer is implicitly creating a DocumentFragment (with nodeType of 11) and setting that as the node's parentNode property.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom