Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reorder html inside multiple divs (jQuery)

I'm using jQuery to make modifications to an otherwise-uneditable page. The page contains several container elements in this format:

<div class="container">
    <div class="A"></div>
    <div class="B">
        <div class="C"></div>
    </div>
    <div class="D"></div>
</div>

For each container, I want to move the content so that A is inside of B, like this:

<div class="container">
    <div class="B">
        <div class="C"></div>
        <div class="A"></div>
    </div>
    <div class="D"></div>
</div>

What's the best way to do this with jQuery? Every solution I can think of would only work for a single container element, as opposed to modifying them all.

like image 374
Cliff Avatar asked Dec 07 '25 10:12

Cliff


1 Answers

$('.container').each(function(){
    $(this).find('.A').appendTo($(this).find('.B'));
});
like image 95
Musa Avatar answered Dec 09 '25 23:12

Musa