---
title: "How to move an element into another element in JavaScript?"  
description: "How to move an element into another element in JavaScript?"  
author: "Anonymous User"  
published: 2021-07-20  
updated: 2023-11-28  
canonical: https://www.mindstick.com/forum/156572/how-to-move-an-element-into-another-element-in-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# 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](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

To move an element from one parent to another in JavaScript, you can use the combination of **appendChild()** and **removeChild()** methods. Here's an example:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Move Element in JavaScript</title>
  <style>
    .container1, .container2 {
      border: 1px solid #ccc;
      padding: 10px;
      margin-bottom: 10px;
    }
  </style>
</head>
<body>

  <div class="container1">
    <p>This is the element to move.</p>
  </div>

  <div class="container2">
    <!-- Target container for the moved element -->
  </div>

  <script>
    // Move the element from container1 to container2
    var elementToMove = document.querySelector('.container1 p');
    var targetContainer = document.querySelector('.container2');

    targetContainer.appendChild(elementToMove);
  </script>

</body>
</html>
```

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.


---

Original Source: https://www.mindstick.com/forum/156572/how-to-move-an-element-into-another-element-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
