We have a 10 divs as our html result:
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
How to put each started 4 divs group into new container by jQuery? I want to get result like this:
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="container">
<div></div>
<div></div>
</div>
Remember that sometimes it will be 0 divs, sometimes 3, sometimes 8, sometimes 14. Thanks for your help.
PS: Yes, I tried wrap, but when i set 4 it is not working with 2 or 7 divs. I tried lot of ifs and sstrange solutions...
var goals = $('.goal-slider').find('.goal');
console.log(goals.length);
goals.each(function(index){
if(index%4 != 0 && index == goals.length) {
goals.slice(index - index%4, index).wrap('<div class="goal-slide"></div>');
}
if(index%4 == 0 && index != goals.length - 1) {
goals.slice(index - 3, index).wrap('<div class="goal-slide"></div>');
}
});