jquerydrupal-6jquery-1.3

How do I wrap each set of two items with jQuery?


I'd like to wrap each set of the <h2> and <table> within their own <div> so I can manipulate the new container with CSS.

Below is a simplified version of the HTML I am working with:

<div>
<h3></h3>
<h2 class="product-line-term-name">Product 1</h2>
<table class="views-view-grid"></table>
<h3></h3>
<h2 class="product-line-term-name">Product 2</h2>
<table class="views-view-grid"></table>
<h3></h3>
<h2 class="product-line-term-name">Product 3</h2>
<table class="views-view-grid"></table>
</div>

Note: I am only working on the theme layer so jQuery (version 1.3.2) and CSS are my tools.


Solution

  • Look at this code

    jQuery.each($('.product-line-term-name'), function(i, val) {
       //Create new element 
        var div = "<div>" + $(this)[0].outerHTML + $(this).next()[0].outerHTML + "</div>";  
    
        $(this).prev().after(div);
        $(this).next().remove();
        $(this).remove();
    });
    

    See fiddle