Mark Richman Mark Richman - 7 months ago 18
Javascript Question

How to move an element into another element?

I would like to move one DIV element inside another. For example, I want to move this (including all children):

<div id="source">
...
</div>


into this:

<div id="destination">
...
</div>


so that I have this:

<div id="destination">
<div id="source">
...
</div>
</div>

Answer

You may want to use the appendTo function (which adds to the end of the element):

$("#source")
    .appendTo("#destination");

Alternatively you could use the prependTo function (which adds to the beginning of the element):

$("#source")
    .prependTo("#destination");