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>
This question is related to
javascript
jquery
html
You can use:
To Insert After,
jQuery("#source").insertAfter("#destination");
To Insert inside another element,
jQuery("#source").appendTo("#destination");
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');});
$('.target')[0].appendChild($('.source')[0]);
Using appendChild removes the .source and places it into target including it's event listeners: https://developer.mozilla.org/en-US/docs/Web/API/Node.appendChild
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">
<label>Name</label>
<input ...>
</div>
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.
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");
Example:
$("#appendTo").click(function() {_x000D_
$("#moveMeIntoMain").appendTo($("#main"));_x000D_
});_x000D_
$("#prependTo").click(function() {_x000D_
$("#moveMeIntoMain").prependTo($("#main"));_x000D_
});
_x000D_
#main {_x000D_
border: 2px solid blue;_x000D_
min-height: 100px;_x000D_
}_x000D_
_x000D_
.moveMeIntoMain {_x000D_
border: 1px solid red;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div id="main">main</div>_x000D_
<div id="moveMeIntoMain" class="moveMeIntoMain">move me to main</div>_x000D_
_x000D_
<button id="appendTo">appendTo main</button>_x000D_
<button id="prependTo">prependTo main</button>
_x000D_
my solution:
MOVE:
jQuery("#NodesToMove").detach().appendTo('#DestinationContainerNode')
COPY:
jQuery("#NodesToMove").appendTo('#DestinationContainerNode')
Note the usage of .detach(). When copying, be careful that you are not duplicating IDs.
You can use following code to move source to destination
jQuery("#source")
.detach()
.appendTo('#destination');
try working codepen
function move() {_x000D_
jQuery("#source")_x000D_
.detach()_x000D_
.appendTo('#destination');_x000D_
}
_x000D_
#source{_x000D_
background-color:red;_x000D_
color: #ffffff;_x000D_
display:inline-block;_x000D_
padding:35px;_x000D_
}_x000D_
#destination{_x000D_
background-color:blue;_x000D_
color: #ffffff;_x000D_
display:inline-block;_x000D_
padding:50px;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div id="source">_x000D_
I am source_x000D_
</div>_x000D_
_x000D_
<div id="destination">_x000D_
I am destination_x000D_
</div>_x000D_
_x000D_
<button onclick="move();">Move</button>
_x000D_
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()
.
You may also try:
$("#destination").html($("#source"))
But this will completely overwrite anything you have in #destination
.
dirty size improvement of Bekim Bacaj answer
div { border: 1px solid ; margin: 5px }
_x000D_
<div id="source" onclick="destination.appendChild(this)">click me</div>_x000D_
<div id="destination" >...</div>
_x000D_
If the div
where you want to put your element has content inside, and you want the element to show after the main content:
$("#destination").append($("#source"));
If the div
where you want to put your element has content inside, and you want to show the element before the main content:
$("#destination").prepend($("#source"));
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:
$("#destination").append($("#source").clone());
// etc.
You can use pure JavaScript, using appendChild()
method...
The appendChild() method appends a node as the last child of a node.
Tip: If you want to create a new paragraph, with text, remember to create the text as a Text node which you append to the paragraph, then append the paragraph to the document.
You can also use this method to move an element from one element to another.
Tip: Use the insertBefore() method to insert a new child node before a specified, existing, child node.
So you can do that to do the job, this is what I created for you, using appendChild()
, run and see how it works for your case:
function appendIt() {_x000D_
var source = document.getElementById("source");_x000D_
document.getElementById("destination").appendChild(source);_x000D_
}
_x000D_
#source {_x000D_
color: white;_x000D_
background: green;_x000D_
padding: 4px 8px;_x000D_
}_x000D_
_x000D_
#destination {_x000D_
color: white;_x000D_
background: red;_x000D_
padding: 4px 8px;_x000D_
}_x000D_
_x000D_
button {_x000D_
margin-top: 20px;_x000D_
}
_x000D_
<div id="source">_x000D_
<p>Source</p>_x000D_
</div>_x000D_
_x000D_
<div id="destination">_x000D_
<p>Destination</p>_x000D_
</div>_x000D_
_x000D_
<button onclick="appendIt()">Move Element</button>
_x000D_
If you want a quick demo and more details about how you move elements, try this link:
http://html-tuts.com/move-div-in-another-div-with-jquery
Here is a short example:
To move ABOVE an element:
$('.whatToMove').insertBefore('.whereToMove');
To move AFTER an element:
$('.whatToMove').insertAfter('.whereToMove');
To move inside an element, ABOVE ALL elements inside that container:
$('.whatToMove').prependTo('.whereToMove');
To move inside an element, AFTER ALL elements inside that container:
$('.whatToMove').appendTo('.whereToMove');
What about a JavaScript solution?
// Declare a fragment:
var fragment = document.createDocumentFragment();
// Append desired element to the fragment:
fragment.appendChild(document.getElementById('source'));
// Append fragment to desired element:
document.getElementById('destination').appendChild(fragment);
Source: Stackoverflow.com