www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to wrap every 4 generatord div's in a parent div

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Posts
    27

    How to wrap every 4 generatord div's in a parent div

    Hi,

    for the jquery cycle script I need to generate a wrap around every 4 dynamicly created div's of the same class.

    for example, If the following posts are generated:

    Code:
    <div id="postscontainer">
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
      <div class="post"></div>
    </div>
    It actually needs to generate this:

    Code:
    <div id="postcontainer">
       <div class="4postwrap">
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
       </div>
       <div class="4postwrap">
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
       </div>
       <div class="4postwrap">
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
       </div>
       <div class="4postwrap">
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
              <div class="post"></div>
       </div>
    </div>
    I've already managed to get this, of which I think i'm getting close:

    Code:
    var $posts= $('.post')
    var numPosts= $posts.length;
    
    for ( i=0; i< numPosts; **AND THEN?**){
     
    // create new div and append pairs of posts into new div- then put the new div into id="postcontainer"
        $('<div class="4postwrap"></div>').append($posts.eq(i)).append($posts.eq(i+1)).appendTo('#postcontainer');
    }
    Can anyone tell me what to fill in at the for loop to make it work?

    Thanks.

    Dan

  2. #2
    Join Date
    Nov 2009
    Posts
    27
    anyone please? Still failing to solve this

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles