I have the following structure, one external div (#results) and about 20 divs with a class event. I want to group every 3 events and wrap a div called outer around them,
<div id="result"> <div class="event"> <div class="date">8 April</div> <div class="eventname">my title</div> <div class="link">my link goes here</div> </div> // lots more events here </div>
Try:
while ($('#result > .event').length > 0) { $('#result > .event:lt(3)').wrapAll('<div class="wrap"></div>') }โโโโ
http://jsfiddle.net/rxxjp/
Here's one approach: http://jsfiddle.net/vM9KM/4/
$('#result').children('div.event:nth-child(3n+1)').each(function(i,el) { $(this).next().andSelf().next().andSelf().wrapAll('<div class="outer">'); });โ
http://api.jquery.com/nth-child-selector/
I used the splice and wrapAll to group 3 wrapAll and wrapped them inside <div class="wrapper" ></div>
splice
wrapAll
<div class="wrapper" ></div>
Demo
var events = $('#result .event'); while (events.length >= 3) { $(events.splice(0, 3)).wrapAll('<div class="wrapper" />'); } //wrap the remaining < than 3 div inside wrapper $(events.splice(0, events.length)).wrapAll('<div class="wrapper" />');
Source: https://habr.com/ru/post/1402901/More articles:How to unit test a grails domain class with relational mapping? - unit-testingThe Javascript order the function is called on - javascriptSetting the path to JAR file classes - javaboost :: transform_iterator not working with std :: bind (& Pair :: first, _1)? - c ++Windows Aero - software disable visual effects - accessibilityDownload resque working without rails? - ruby โโ| fooobar.comWhat DNS record should be configured to use a domain name without www? - dnsYoutube iframe embedded video doesn't work (black screen) - cssruby resque no load on rails - activerecordAligning text in a table with CSS - htmlAll Articles