Wrap two divs in one container
I'm having trouble wrapping two adjacent divs in one parent container. For example take the following html:
- <div>
- <div class="line"> hi </div>
- <div class="line"> hi again </div>
- <div class="line"> hi </div>
- <div class="line"> hi again </div>
- </div>
I need every pair of ".line" divs to be wrapped in a parent div, so it will look like this:
- <div>
- <div class="new">
- <div class="line"> hi </div>
- <div class="line"> hi again </div>
- </div>
- <div class="new">
- <div class="line"> hi </div>
- <div class="line"> hi again </div>
- </div>
- </div>
I can't seem to get .wrap() to work, it will only select the individual .line divs. I also can't use .before() or .after because when I insert the first <div class="new">, jquery automatically closes it. For example this doesn't work:
- $(".line:even").before("<div class=\"new\">");
- $(".line:odd").after("</div>");
The opening div is automatically closed by jquery on insert, and the closing div is ignored
Anyone know of another way to do it?