How to move from one div or element to another div or any other element via javascript?
How to move an element into another element in JavaScript?
35520-Jul-2021
Updated on 28-Nov-2023
Home / DeveloperSection / Forums / How to move an element into another element in JavaScript?
How to move from one div or element to another div or any other element via javascript?
Aryan Kumar
28-Nov-2023To move an element from one parent to another in JavaScript, you can use the combination of appendChild() and removeChild() methods. Here's an example:
In this example:
The <p> element inside the first container (.container1) is selected using document.querySelector().
The target container (.container2) is also selected.
The appendChild() method is used to append the selected element to the target container. This automatically removes the element from its current parent.
As a result, the paragraph element will be moved from .container1 to .container2. Adjust the selectors based on your HTML structure and the elements you want to move.
Keep in mind that this is a basic example, and in a real-world scenario, you might want to perform additional checks or modifications depending on your specific requirements.