Reputation: 127
how can I just append an object to another in the same object? Like this:
<div class="foo">
<div class="bar">Foo</div>
<div class="lorem">Bar</div>
</div>
<div class="foo">
<div class="bar">Foo</div>
<div class="lorem">Bar</div>
</div>
Now i will but the first div into the second one, so like:
$('.bar').appendTo('.lorem');
Here the Foo will appear on every bar on the website. In this example it will appear twice (like this). Is there a possibility to jump just the Foo into the .lorem-div which is in the same container?
I tried it with something like $(this).$('.bar').appendTo(this).('.lorem')
but it doesn't work.
Upvotes: 0
Views: 22
Reputation: 67197
Try to iterate over the elements with class bar
and append it to its adjacent sibling element by using next()
. And the important point is that .next()
has to be invoked over $(this)
,
$('.bar').each(function(){
$(this).appendTo($(this).next(".lorem"))
});
Upvotes: 1