I want to swap 2 divs using jquery but when I tried to swap it using insertAfter() in gets inserted twice. Just want to specify that each parent element needs to only swap the divs once.
So in each parent element wcmsd-step-container I need to insert the wcmsd-checkbox-item-description class below the wcmsd-step-title class. But it gets duplicated twice when swapping it.
Can anyone point what's the right function to use on my jQuery code?
jQuery(document).ready(function() {
jQuery('.wcmsd-step-container').each(function() {
jQuery('.wcmsd-checkbox-item-description').insertAfter(jQuery('.wcmsd-step-title'));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--Step 1-->
<div class="wcmsd-step-container">
<h2 class="wcmsd-step-title">What type of photo do you want?</h2>
<div class="wcmsd-step-content">
<label class="wcmsd-checkbox-item-title">choose your photo</label>
<p class="wcmsd-checkbox-item-description">Select up to 122. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi accumsan faucibus proin nisi, risus vehicula. Fames metus, metus consectetur.</p>
</div>
</div>
<!--Step 2-->
<div class="wcmsd-step-container">
<h2 class="wcmsd-step-title">What type of video do you want?</h2>
<div class="wcmsd-step-content">
<label class="wcmsd-checkbox-item-title">choose your video</label>
<p class="wcmsd-checkbox-item-description">Select up to 322. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi accumsan faucibus proin nisi, risus vehicula. Fames metus, metus consectetur.</p>
</div>
</div>