jQuery - Get content between 2 tags and wrap this in new elements
<p>I've got a problem and I just don't know how to solve this (relatively) simple function. What i want to achieve is the following. My client is using a CMS wich gives me this output.</p>

<pre><code>&lt;div class="foldOutEnabled"&gt;

    &lt;h2&gt;Title 01&lt;/h2&gt;

        &lt;p&gt;1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tortor ligula, pulvinar eget vestibulum quis, lobortis et quam.&lt;/p&gt;

        &lt;h3&gt;subtitle 01&lt;/h3&gt;
        &lt;p&gt;1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tortor ligula, pulvinar eget vestibulum quis, lobortis et quam.&lt;/p&gt;

    &lt;h2&gt;Title 02&lt;/h2&gt;

        &lt;p&gt;1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tortor ligula, pulvinar eget vestibulum quis, lobortis et quam.&lt;/p&gt;

        &lt;h3&gt;subtitle 02&lt;/h3&gt;
        &lt;p&gt;1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tortor ligula, pulvinar eget vestibulum quis, lobortis et quam.&lt;/p&gt;

&lt;/div&gt;
</code></pre>

<p>What i am trying to do is get all HTML between the 'h2' tags and wrap this in another div (so jQUery can make an accordion menu with it). For this to work i need the output to become this,</p>

<pre><code>&lt;div class="foldOutItem"&gt;
    &lt;div class="foldOutItemTitle"&gt;
        &lt;h2&gt;
            &lt;a href="#"&gt;&lt;img class="foldoutArrow" width="17" height="20" alt="" src="../images/footer-arrow.png"&gt;Title 01&lt;/a&gt;
        &lt;/h2&gt;
    &lt;/div&gt;
    &lt;div class="foldOutItemContent"&gt;

        &lt;p&gt;1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tortor ligula, pulvinar eget vestibulum quis, lobortis et quam.&lt;/p&gt;

        &lt;h3&gt;subtitle 01&lt;/h3&gt;
        &lt;p&gt;1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tortor ligula, pulvinar eget vestibulum quis, lobortis et quam.&lt;/p&gt;

    &lt;/div&gt;
&lt;/div&gt;

etc...
</code></pre>

<p>After trying many different solutions and ways i've come as far is the following,</p>

<pre><code>if ($('.foldOutEnabled').length) {

    var newHtml = '&lt;div class="foldOutTitle"&gt;&lt;span&gt;&lt;a id="foldOutAll" href="#"&gt;Alles uitklappen&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;';

    $('.foldOutEnabled h2, .foldOutEnabled h3, .foldOutEnabled table, .foldOutEnabled p, .foldOutEnabled span').each(function(i) {

        if (this.nodeName == 'H2')
        {
            if (i != 0) { newHtml += '&lt;/div&gt;&lt;/div&gt;'; }
            newHtml += '&lt;div class="foldOutItem"&gt;&lt;div class="foldOutItemTitle"&gt;&lt;h2&gt;&lt;a href="#"&gt;&lt;img src="../images/footer-arrow.png" alt="" width="17" height="20" class="foldoutArrow" /&gt;' + $(this).html() + '&lt;/a&gt;&lt;/h2&gt;&lt;/div&gt;&lt;div class="foldOutItemContent"&gt;';
        } else {
            newHtml += $(this).html();
        }

    });

    newHtml += '&lt;/div&gt;&lt;/div&gt;';
    $('.foldOutEnabled').html(newHtml);
}
</code></pre>

<p>In my opinion this can be improved but it works except that the .html() strips all the html tags. So all the 'p' and 'h3' and what ever more tags the content contains are gone.</p>

<p>Now my question, how can i prevent this?</p>
