Jayson LaFrance
Jayson LaFrance

Reputation: 47

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.

Upvotes: 1

Views: 94

Answers (3)

A S M.Abdur Rab
A S M.Abdur Rab

Reputation: 46

I think the following code will serve your purpose for jQuery 1.3.2:

var h3_length = $('h3').length;

for(var i= h3_length - 1; i>= 0; i--) {
    $('h3').eq(i).nextAll().not(".test, h3").wrapAll('<div class="test" />');
}

Upvotes: 0

Silagy
Silagy

Reputation: 3083

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

Upvotes: 0

Eli
Eli

Reputation: 14827

You can make use of nextUntil and wrapAll in this case:

$('h3').each(function(){
    $(this).nextUntil('h3').wrapAll('<div class="example" />');
});

Demo: http://jsfiddle.net/9w9Sp/

Upvotes: 2

Related Questions