How to add a DIV to each 3 paragraphs (<p>)

Asked

Viewed 167 times

2

Well what I want is simple. I need to add one div every 3 paragraphs. Remembering that these paragraphs will be in another div (in case it would be the content div).

It looks like this website, where the advertisement is located.

Would that be:

<div class="conteudo">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <div class="anuncio">Div inserida</div>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <div class="anuncio">Div inserida</div>
    <p>&nbsp;</p>
</div>
  • Can you give an example of the HTML you refer to? Can you [Edit] the question and join the code here.

  • Well, I added the code.

2 answers

5

You can select all p and then use their index inside a loop to count/group three by three. Joining this the .insertAfter you got what you need:

$('p').each(function(i) {
    var pos = i + 1;
    if (pos % 3 == 0) {
        $('<div/>', {
            class: 'anuncio',
            text: 'Div inserida!'
        }).insertAfter(this);
    }
});

jsFiddle: https://jsfiddle.net/k26973vz/

  • It worked perfectly. Thank you very much. I tried to mark as solved but it seems I can’t yet. Vlw

  • How could I add a script inside that div?

  • @Can Helmesvs give an example of what script you want to run? If you want to insert content from another website it might be a case of using iframe.

  • It’s an ad: <script type="text/javascript">&#xA;<!--&#xA;descrColor="666666";titleColor="333333";urlColor="333333";borderColor="FFFFFF";bgColor="FFFFFF";altColor="FFFFFF";coddisplaysupplier="dd7bca939f064a9e89c7863c490e1de2";formatId="1";numads="4";deslabel="post";type="1";&#xA;-->&#xA;</script>&#xA;<script type="text/javascript" src="http://adrequisitor-af.lp.uol.com.br/uolaf.js"></script>

  • @Helmesvs is what you’re looking for? https://jsfiddle.net/sn0zz83v/ I advise against having global variables that way. It would give an interesting question "how to configure/customize script variables".

  • It is only printing "ad". But the ad itself does not appear.

  • Someone has a solution?

  • @Helmesvs that is a problem in your script that you are loading via url. I think your original question is solved, you have two answers. If you want you can ask a new question for the problem you’re having right now.

Show 3 more comments

3

You can use a formula (an + b) the selector to specify the elements you want to select. In this formula a represents the cycle size, n is a counter (starting at 0), and b is a deviation value.

See more

$div = $('<div class="anuncio">DIV INSERIDA</div>');
$div.insertAfter('.conteudo p:nth-child(3n+0)');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="conteudo">
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
  <p>PARÁGRAFO</p>
</div>

  • 1

    Good idea nth-child(3n+0) as selector... +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.