I'm struggling to move an element into the next element in the DOM. I can move it but it keeps repeating itself into the other div's with the same class.
This is my HTML:
<p>The first text that needs to be moved</p>
<div class="tmb">
<span class="price">500</span>
</div>
<p>Second text that needs to be moved</p>
<div class="tmb">
<span class="price">500</span>
</div>
What I want to happen is that the first p element will be inserted after the first .price element. The second p element will be inserted after the second .price element. I can't give the p elements a class because they are created dynamically.
I have this jQuery:
$(".tmb").prev("p").insertAfter("span.price");
But this moves the p element after every .price element, which is logically. I just want it to move after the first .price element that it comes across in the DOM.
To achieve this you could loop through all
pelements as there are multiple, and then useappendTo()to add them to their related.tmb. Try this:Alternatively, you could loop through all the
.tmbelements which are preceded by apandinsertAfter()the.pricecontained there.