How to move an element into another element?

Mark Richman Source

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

<div id="source">

into this:

<div id="destination">

so that I have this:

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


answered 9 years ago Andrew Hare #1

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


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



$("#appendTo").click(function() {
$("#prependTo").click(function() {
#main {
  border: 2px solid blue;
  min-height: 100px;

.moveMeIntoMain {
  border: 1px solid red;
<script src=""></script>
<div id="main">main</div>
<div id="moveMeIntoMain" class="moveMeIntoMain">move me to main</div>

<button id="appendTo">appendTo main</button>
<button id="prependTo">prependTo main</button>

answered 6 years ago kjc26ster #2

I just used:


Which I grabbed from here.

answered 6 years ago sbaaaang #3

If the div where you want to put your element has content inside, and you want the element to show after the main content:


If the div where you want to put your element has content inside, and you want to show the element before the main content:


If the div where you want to put your element is empty, or you want to replace it entirely:

$("#element").html('<div id="source">...</div>');

If you want to duplicate an element before any of the above:

// etc.

answered 6 years ago Tamas #4

You may also try:


But this will completely overwrite anything you have in #destination.

answered 5 years ago Subrahmanyam #5

You can use:

To Insert After,


To Insert inside another element,


answered 5 years ago Alejandro Illecas #6

my solution:





Note the usage of .detach(). When copying, be careful that you are not duplicating IDs.

answered 4 years ago spetsnaz #7

I noticed huge memory leak & performance difference between insertAfter & after or insertBefore & before .. If you have tons of DOM elements, or you need to use after() or before() inside a MouseMove event, the browser memory will probably increase and next operations will run really slow. The solution I've just experienced is to use inserBefore instead before() and insertAfter instead after().

answered 4 years ago HMR #8

Old question but got here because I need to move content from one container to another including all the event listeners.

jQuery doesn't have a way to do it but standard DOM function appendChild does.

//assuming only one .source and one .target
$('.source').on('click',function(){console.log('I am clicked');});

Using appendChild removes the .source and places it into target including it's event listeners:

answered 4 years ago Dan #9

If you want a quick demo and more details about how you move elements, try this link:

Here is a short example:

To move ABOVE an element:


To move AFTER an element:


To move inside an element, ABOVE ALL elements inside that container:


To move inside an element, AFTER ALL elements inside that container:


answered 3 years ago Subodh Ghulaxe #10

You can use following code to move source to destination


try working codepen

function move() {
  color: #ffffff;
  color: #ffffff;
<script src=""></script>
<div id="source">
I am source

<div id="destination">
I am destination

<button onclick="move();">Move</button>

answered 3 years ago Ali Bassam #11

What about a JavaScript solution?

Declare a fragment:

var fragment = document.createDocumentFragment();

Append desired element to the fragment:


Append fragment to desired element:


Check it out.

answered 2 years ago Bekim Bacaj #12

Ever tried plain JavaScript... destination.appendChild(source); ?

onclick = function(){ destination.appendChild(source); }
div{ margin: .1em; } 
#destination{ border: solid 1px red; }
#source {border: solid 1px gray; }
<!DOCTYPE html>


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


answered 3 weeks ago RayLuo #13

For the sake of completeness, there is another approach wrap() or wrapAll() mentioned in this article. So the OP's question could possibly be solved by this (that is, assuming the <div id="destination" /> does not yet exist, the following approach will create such a wrapper from scratch - the OP was not clear about whether the wrapper already exists or not):

$("#source").wrap('<div id="destination" />')
// or
$(".source").wrapAll('<div id="destination" />')

It sounds promising. However, when I was trying to do $("[id^=row]").wrapAll("<fieldset></fieldset>") on multiple nested structure like this:

<div id="row1">
    <input ...>

It correctly wraps those <div>...</div> and <input>...</input> BUT SOMEHOW LEAVES OUT the <label>...</label>. So I ended up use the explicit $("row1").append("#a_predefined_fieldset") instead. So, YMMV.

comments powered by Disqus