Reputation: 3213
I have the following code:
<div class="product">
<div class="description"></div>
<div class="image"></div>
</div>
<div class="product">
<div class="description"></div>
<div class="image"></div>
</div>
<div class="product">
<div class="description"></div>
<div class="image"></div>
</div>
And Im trying to select all .image elements and move them before each .product element.
When I use the following command, it takes all the images and moves them before the first .product. Im stuck, I tried a foreach - that didnt seem to work.
$('.product').before($('.image'));
Any ideas?
Upvotes: 1
Views: 3436
Reputation: 15478
I think this may work too:
$('.product').each(function (index) {
$(this).before($('.image').eq(index));
});
Upvotes: 4
Reputation: 253308
I'd suggest:
$('.image').each(
function(){
$(this).insertBefore($(this).closest('.product'));
});
References:
Upvotes: 14