Firstly, I cannot access the HTML. It's generated by a system I cannot edit.
I have a list like this:
<ol>
<li class="odd a one">List Item (type1)</li>
<li class="even b two">List Item (type1)</li>
<li class="odd c three">List Item (type1)</li>
<li class="even d one">List Item (type1)</li>
<li class="odd a two">List Item (type1)</li>
<li class="even b three">List Item (type1)</li>
<li class="odd c one">List Item (type1)</li>
<li class="even d two">List Item (type2)</li>
<li class="odd a three">List Item (type2)</li>
<li class="even b four">List Item (type2)</li>
</ol>
I want to split this list and put a heading in using jQuery like so:
$('li:contains("type2")').first().before('</ol><h2>Type 2 starts here</h2><ol>')
However, the code that results is this (according to Firebug):
<ol>
<li class="odd a one">List Item (type1)</li>
<li class="even b two">List Item (type1)</li>
<li class="odd c three">List Item (type1)</li>
<li class="even d one">List Item (type1)</li>
<li class="odd a two">List Item (type1)</li>
<li class="even b three">List Item (type1)</li>
<li class="odd c one">List Item (type1)</li>
<h2>Type 2 starts here</h2>
<ol></ol>
<li class="even d two">List Item (type2)</li>
<li class="odd a three">List Item (type2)</li>
<li class="even b one">List Item (type2)</li>
</ol>
Is there a way of doing this to generate the following code?:
<ol>
<li class="odd a one">List Item (type1)</li>
<li class="even b two">List Item (type1)</li>
<li class="odd c three">List Item (type1)</li>
<li class="even d one">List Item (type1)</li>
<li class="odd a two">List Item (type1)</li>
<li class="even b three">List Item (type1)</li>
<li class="odd c one">List Item (type1)</li>
</ol>
<h2>Type 2 starts here</h2>
<ol>
<li class="even d two">List Item (type2)</li>
<li class="odd a three">List Item (type2)</li>
<li class="even b one">List Item (type2)</li>
</ol>
Here's a fiddle for it all: http://jsfiddle.net/a78pT/1/