S
S
sazhyk2018-01-30 08:20:43
JavaScript
sazhyk, 2018-01-30 08:20:43

How to add an element to a page at a specific position?

Elements are dynamically added to the page. There can be any number of them.

markup

<div class="main">
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
</div>


I need to add a new block after each, for example, the third, so that it is like this
Delimited markup

<a href="">Add divider</a>
<div class="main">
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="divider"></div> <!--This is divider-->
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
    <div class="divider"></div> <!--This is divider-->
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
</div>

But do not add it at the end , even if there are the right number of elements!

How can I implement this in jQuery or JavaScript on button click?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kreotech, 2018-01-30
@sazhyk

https://jsfiddle.net/kreoTech/4k56o26p/1/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question