2

I need to wrap every 4 .product divs in a row within a <li> tag so that when there's:

<ul>
  <div class="product">...</div>
  <div class="product">...</div>
  <div class="product">...</div>
  <div class="product">...</div>
  <div class="product">...</div>
  <div class="product">...</div>
</ul>

it gets turned in to a:

<ul>
  <li>
    <div class="product">...</div>
    <div class="product">...</div>
    <div class="product">...</div>
    <div class="product">...</div>
  </li>
  <li>
    <div class="product">...</div>
    <div class="product">...</div>
  </li>
</ul>

In the example I've given 6 products because it must close the wrapping if those are the last elements anyway.

Can you please show me how this can be done with jquery?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
Xeen
  • 6,955
  • 16
  • 60
  • 111
  • Why would the menu have that structure in the first place? – Paulie_D Mar 10 '15 at 10:52
  • 1
    Have you tried anything? Which bit are you finding difficult? It's just: Get all product divs, loop them, add them to a `
  • ` (which you add to the `
      `), and every 4 loops change to a new `
    • `
  • – musefan Mar 10 '15 at 10:52
  • Need to fix source. You have invalid markup. `
    ` is not a valid child of `
      ` therefore you can't rely on all browsers to render them inside the `
        `
    – charlietfl Mar 10 '15 at 10:54