title: Detecting when a DOM element's parent changes
body: <p>Is there a DOM event that fires when an element's <code>parentElement</code> changes? If not, is there any way better than polling with a timeout?</p>
  
  <p>I'm specifically interesting in knowing when the <code>parentElement</code> changes from <code>null</code> to some defined element. That is, when a DOM element is attached to the document tree somewhere.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Given the questions in the comments, here is an example that shows how to create an element with a <code>null</code> <code>parentElement</code>:</p>
  
  <pre><code>var element = document.createElement('div');
  
  console.assert(element.parentElement == null);
  </code></pre>
  
  <p>The parent is only set once it's added to the DOM:</p>
  
  <pre><code>document.body.appendChild(element);
  
  console.assert(element.parentElement != null);
  </code></pre>
  
  <p>Note too that elements created using jQuery will also have a <code>null</code> parent when created:</p>
  
  <pre><code>console.assert($('&lt;div&gt;&lt;/div&gt;').get(0).parentElement == null);
  </code></pre>