How to move HTML element

iroel picture iroel · Jul 28, 2010 · Viewed 37.7k times · Source

How to move HTML element to another element. Note that, I don't mean moving element's position. Consider this HTML code:

<div id="target"></div>
<span id="to_be_moved"></span>

I want to move "to_be_moved" to "target" so "target" has child "to_be_moved" now. So the result should be like this:

<div id="target"><span id="to_be_moved"></span></div>

I've searched in google (especially using prototype framework) but all I've got is moving position, not as I want. Thanks before.

Answer

meder omuraliev picture meder omuraliev · Jul 28, 2010
document.getElementById('target').appendChild(  document.getElementById('to_be_moved') )