I want a code that moves the span in item-2 to after of span in item-1. I want this transition to happen in the parent element of each span, i.e. items-wrapper
<div class="wrapper">
<div class="items-wrapper">
<div class="item-1">
<span>element</span>
</div>
<div class="item-2">
<span>element</span>
</div>
</div>
<div class="items-wrapper">
<div class="item-1">
<span>element</span>
</div>
<div class="item-2">
<span>element</span>
</div>
</div>
my code
$(".item-2 span").each(function() {
$(this).parent().insertAfter(".item-1 span");
});
Here you go.
parent()goes only 1 step ahead so as per your requirement,$(this).parent().parent().find(".item-1");to find actual dom to useinsertAfter.Example:
Alternative using
appendTo()which insert every element in the set of matched elements to the end of the target.Example: