Pete
Pete

Reputation: 532

Grouping four different divs into one

So I found this code below which groups div's:

var theDivs=  $('.testimonials');
var  numOfLines= theDivs.length;
for (i=0; i< numOfLines; i=i+3){
  theDivs.eq(i).add(theDivs.eq(i+1)).add(theDivs.eq(i+2)).wrapAll('<div class="slide" />') ;
}

It works lovely, but it only groups 3 divs, I'd like it to group 4.

I'm kicking myself about asking this question as I'm sure it's a very easy fix. I've tinkered with the numbers above, but just couldn't get it to work.

Greatly appreciate any assistance.

Upvotes: 0

Views: 50

Answers (2)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85575

How about using lt jquery method:

$(".testimonials:lt(4)").wrapAll('<div class="slide" />');

Or using slice:

$('.testimonials').slice(0,5).wrapAll('<div class="slide" />');

Upvotes: 3

R. Schifini
R. Schifini

Reputation: 9313

Try these changes

var theDivs=  $('.testimonials');
var  numOfLines= theDivs.length;
for (i=0; i< numOfLines; i=i+4){
  theDivs.eq(i).add(theDivs.eq(i+1)).add(theDivs.eq(i+2)).add(theDivs.eq(i+3)).wrapAll('<div class="slide" />') ;
}

Upvotes: 3

Related Questions