How to move HTML element

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.

document.getElementById('target').appendChild(  document.getElementById('to_be_moved') )

Assuming you’re working with native DOM elements, the Javascript method .appendChild will suit your needs.

In native Javascript, document.getElementByID is probably your best bet in getting the DOM element, so…

var target = document.getElementById('target')
document.getElementById('to_be_moved').appendChild(target)


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Copy to clipboard BINARY data in browsers

Similar Posts