title: jQuery - make an element last in the DOM (before close of body) even after dynamically generated elements
body: <p><strong>tl;dr:</strong> How do I get a DOM element, that is made visible by a click handler, to be the last element before close of <code>body</code> even if dynamically generated elements are being appended? </p>
  
  <p>Essentially I want to have the click that opens the element also move it in the DOM to be last before body close even after some dynamic elements have been appended.</p>
  
  <hr>
  
  <p>My issue stems from a modal that is being opened within another modal while there are multiple modals on one page. One catch is that the submodals exist in the DOM at page load while the primary modals are being instantiated onclick (the submodals use different code to generate while the primary ones use jQuery UI dialog).</p>
  
  <p>If you open a modal and then open the submodal all is well. If you open a second modal then close it when you reopen that first modal the submodal will not appear as it is hidden below the second modal. If I append these submodals to the body, thus putting them after any generated modals they appear fine (but that creates other issues). In spite of what it looks like this does not appear to be a z-index problem (I've tried everything related to that with no luck), it appears to be related to where these modals are positioned in the DOM.</p>
  
  <p>Code might be clearer:</p>
  
  <p>This is what the html looks like on page load:</p>
  
  <pre><code>&lt;div id='submodal_1' class='submodal'&gt;foo&lt;/div&gt;
  &lt;div id='submodal_2' class='submodal'&gt;bar&lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>After you click to open the first primary modal window:</p>
  
  <pre><code>&lt;div id='submodal_1'&gt;subfoo&lt;/div&gt;
  &lt;div id='submodal_2'&gt;subbar&lt;/div&gt;
  &lt;div id='primary_modal_1' class='ui-dialog' style='display:block'&gt;foo&lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>After you click to open that modal's submodal I have to move the submodal to be below the primary modal otherwise it will not appear (and again, z-index does not effect this blocking):</p>
  
  <pre><code>$('#submodal_1').insertAfter('#primary_modal_1');
  </code></pre>
  
  <p>At this point if you close that <code>#primary_modal_1</code> it will be hidden but still there in the DOM. If you open a second the code will now look like:</p>
  
  <pre><code>&lt;div id='submodal_2' class='submodal'&gt;subbar&lt;/div&gt;
  &lt;div id='primary_modal_1' class='ui-dialog' style='display:none'&gt;foo&lt;/div&gt;
  &lt;div id='submodal_1' class='submodal'&gt;subfoo&lt;/div&gt;
  &lt;div id='primary_modal_2' class='ui-dialog' style='display:block'&gt;bar&lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>At this point if you reopen the first primary modal and try to open its submodal the submodal will not appear. However if you move the submodal to be below that last primary modal, like so:</p>
  
  <pre><code>&lt;div id='submodal_2' class='submodal'&gt;subbar&lt;/div&gt;
  &lt;div id='primary_modal_1' class='ui-dialog' style='display:block'&gt;foo&lt;/div&gt;
  &lt;div id='primary_modal_2' class='ui-dialog' style='display:none'&gt;bar&lt;/div&gt;
  &lt;div id='submodal_1' class='submodal'&gt;subfoo&lt;/div&gt; //&lt;-- moved this line
  &lt;/body&gt;
  </code></pre>
  
  <p>The submodal will apear correctly. I want to ensure that the submodals are always last in the DOM, regardless of how many primary modals are appended.</p>