There is the following structure:

<div class='title'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> <div class='title'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> <div class='title'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> 

The number of list elements in each of the "blocks" is different and the number of "blocks" may also be different. So here is how to do this with jQuery:

 <div class='block'> <div class='title'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> </div> <div class='block'> <div class='title'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> </div> <div class='block'> <div class='title'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> <div class='list'></div> </div> 
  • Are these all nested blocks? Show where they are closing. - Zhukov Roman
  • oh, I apologize, now I will correct it) - Artyom Gribkov

3 answers 3

We run a cycle on all these divas, as we saw the title, create a new wrapper and transfer all divas to it until the next title is encountered.

http://jsfiddle.net/RR9M7/1/

    Here is another alternative .

       var a,b = 0; $('#parent div').each(function(){ if($(this).hasClass('title')){ b = $(this).index(); $('#parent div').slice(a, b).wrap('<div class="block"></div>'); a = b; } }); 

      in haste of course. but the idea is to sort through the entire block line by line. But I would advise to solve such problems on the server.

      • does not work. The index at each iteration is recalculated, taking into account the wrappers. vrabsya every div. The last group is not counted. - Yura Ivanov