title: Run javascript function when replaceWith finishes loading
body: <p>I have jQuery <code>replaceWith</code> call, and <strong>I want to pop up an alert only when the replaceWith finishes loading</strong>. </p>
  
  <p>To achieve this I have this very naive javascript implementation:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function (){
    $("#myDiv").click(function () {
      
      $("#myDiv").replaceWith("&lt;div&gt;Hello World!!!&lt;/div&gt;");
      
      alert("done");
    });
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div id="myDiv"&gt;Hello!&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The problem is that in this case the alert will pop-up independently of the time that the <code>replaceWith</code> takes. If it is fast, no problem, but if the <code>replaceWith</code> takes several seconds to load (which is the real case) then the pop-up appears way before, and I want to avoid that.</p>
  
  <p>How can I achieve the behaviour I am looking for?</p>