Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html structure affecting element cloning

Tags:

html

jquery

clone

So, I have bunch of elements inside parent elements:

<div class="copyFrom">
    <div class="copyThese">copyThese1</div>
</div>
<div class="copyFrom">
    <div class="copyThese">copyThese2</div>
</div>

Also, I have separate .Parent element in which I append .copyHere elements to correspond the amount of .copyFrom elements

<div class="Parent">
    <div class="copyHere"></div>
    <div class="copyHere"></div>
</div>

Aaaalso, Inside .copyHere I clone every separate .copyThese elements

<div class="Parent">
    <div class="copyHere">
        <div class="copyThese">copyThese1</div>
    </div>
    <div class="copyHere">
        <div class="copyThese">copyThese2</div>
    </div>
</div>

Now, the problem is:

For some reason, if .Parent is below all the .copyFrom elements The .copyThese elements are cloned in just fine.

But.. If .Parent element is above The .copyFrom elements, the cloning of .copyThese elements goes haywire.

I need to have .Parent element both above and below. ( Without weird cloning problems.)

jsfiddle:

http://jsfiddle.net/lollero/mZhUG/2/ - Above - Correct

http://jsfiddle.net/lollero/mZhUG/3/ - Below - Problem

like image 579
Joonas Avatar asked Dec 05 '25 14:12

Joonas


1 Answers

you should do:

  var copyThese = $('.copyFrom .copyThese');

because you are adding .copyThese elements at every iteration and keep cloning the same element. This doesn't happen if you add the elements After the one already present in the dom and so the problem is present only if .parent is prepended

fiddle here:

http://jsfiddle.net/mZhUG/4/

like image 93
Nicola Peluchetti Avatar answered Dec 07 '25 03:12

Nicola Peluchetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!