views:

281

answers:

1

Hi. I have a simple unordered list with 16 list items. I want to add a new list item after every fourth existing list item using jquery

How would I do that? Code below:

<ul>
    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>

    <li class="clear">This is what I want to add</li>

    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>

    <li class="clear">This is what I want to add</li>

    and so on...
</ul>
+5  A: 

Using the :nth-child selector (documentation):

$('ul li:nth-child(4n)').after('<li class="clear">This is what I want to add</li>')
htanata
@htanata thanks man!
mtwallet