In HTML, the appendChild
method is used to move an element. This method will append the given node into the node in which the method is invoked.
appendChild(childToAppend);
This method takes a parameter, childToAppend
, which represents the node to be appended.
The method returns the appended node. In the case that the childToAppend
is a document fragment, then an empty document fragment is returned.
The below code demonstrates how to use the appendChild
method to move a div
element into another element.
div
element with the ID, element_to_move
. div
element with the ID, destination_parent
. This element contains one p
(paragraph) element as a child. We'll move the div
element with the ID, element_to_move
, into this div
element. elementToMove
, and assign the div
element with the ID, element_to_move
, as a value.destinationElement
, and assign the div
element with the ID, destination_parent
, as a value.destinationElement
using the outerHTML
property.appendChild
method on destinationElement
with elementToMove
as an argument. This will move the element from the body
and append it as the last child of the destinationElement
.