jQuery wrap every X elements in a set with a div


/ Published in: jQuery
Save to your folder(s)

myEls should be the selector statement for whatever elements you want to effect. The example wraps every 3.


Copy this code and paste it in your HTML
  1. var myEls = $('.main > li');
  2. var numEls = myEls.length;
  3.  
  4. for ( i=0; i<numEls; i=i+3 ) {
  5. myEls.eq(i)
  6. .add( myEls.eq(i+1) )
  7. .add( myEls.eq(i+2) )
  8. .wrapAll('<div class="wrapper"/>');
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.