title: javascript Extracting an element from HTML - Keeping parent/child structure
body: <p>Having the following html:</p>
  
  <pre><code>&lt;html&gt;
      &lt;body&gt;
          &lt;div id="first"&gt;
              1
              &lt;div class="first-first"&gt;
                  &lt;p&gt;1.1&lt;/p&gt;
              &lt;/div&gt;
              &lt;div class="first-second"&gt;
                  &lt;p&gt;1.2&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div id="second"&gt;
              2
              &lt;div class="second-first"&gt;
                  &lt;p&gt;2.1&lt;/p&gt;
              &lt;/div&gt;
              &lt;div class="second-second"&gt;
                  &lt;p&gt;2.2&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How could I extract an element from the HTML keeping his parent-child structure? e.g. if I want to extract the class '<strong>second-first</strong>', I will have a html object that looks like this:</p>
  
  <pre><code>&lt;html&gt;
      &lt;body&gt;
          &lt;div id="second"&gt;
              2
              &lt;div class="second-first"&gt;
                  &lt;p&gt;2.1&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So <strong>#first</strong> and <strong>.second-second</strong> will be removed.</p>
  
  <p><a href="http://jsfiddle.net/3G7Dk/1/" rel="nofollow">jsfiddle</a></p>
  
  <p>I've tried different approaches but with no success at all, also if someone knows how to do that with a time complexity less than O(n^2) would be great to know.</p>
  
  <p>Thanks a lot!</p>