Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append/prepend to the container

I have the following HTML code:

<div class="container">
    <div><div>...</div><a class="child" href="#">Example</a></div>..</div></div>
</div>
<div class="container">
    <div><div>...</div><a class="child" href="#">Example</a></div>..</div></div>
</div>
.
.
.
<div class="container">
    <div><div>...</div><a class="child" href="#">Example</a></div>..</div></div>
</div>

And I have the following JavaScript script:

$('a.child').prependTo('div.container');

But this code, instead of making each a element prepended to their own container, makes all a elements prepended to each of the containers. How can I fix this?

Thanks!

like image 225
federico-t Avatar asked Apr 14 '26 03:04

federico-t


1 Answers

Iterate over each a.child, and prepend it.

$('a.child').each(function ()
{
    var $this = $(this);
    $this.prependTo($this.closest('div.container'));
});

http://jsfiddle.net/mattball/tRNUS

like image 100
Matt Ball Avatar answered Apr 15 '26 15:04

Matt Ball



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!