jquery - Wrapping a div around every three divs -



jquery - Wrapping a div around every three divs -

say have 6 kid divs , don't have unique identifiers:

<div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div>

with jquery, want wrap every set of 3 <div class="parent"></div>. render as:

<div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> <div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div>

what's easiest way this?

wow, nice challenging question =)

while(($children = $(':not(.parent)>.child:lt(3)')).length) { $children .parent() .append( $('<div class="parent"></div>') .append($children.remove()) ); } edit: didn't know wrapall method, so: while(($children = $(':not(.parent)>.child:lt(3)')).length) { $children.wrapall($('<div class="parent"></div>')); }

jquery wrap

Comments

Popular posts from this blog

java - How to pass parameters between Request-Scoped Controllers? -

actionscript 3 - Flex: moving a point with rotation doesnt change the point XY? -

ms access - C# - Using OleDbParameter on table name -