The Wayback Machine - https://web.archive.org/web/20241003223418/https://www.geeksforgeeks.org/how-to-move-one-div-element-inside-another-using-jquery/
Open In App

How to move one DIV element inside another using jQuery ?

Last Updated : 06 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will learn to move one HTML div element inside another using jQuery. 

The HTML div element is used to define a division or a section in an HTML document.

Method used:  

  • parent(): This method is used to get the parent of each element in the current set of matched elements, optionally filtered by a selector.
  • detach(): This method is used to removes the selected elements from the DOM tree including its all text and child nodes but it keeps the data and the events.
  • attr(): This method is used to set or return attributes and values of the selected elements.
  • appendTo(): This method is used to insert HTML elements at the end of the selected elements.

Approach: 

  • Create the HTML page with the various division within the division element.
  • Next, create a function using the above methods to move the inner division element onclick event.

Example:

HTML
<!DOCTYPE html>
<html>
  <head>
    <script src=
"https://code.jquery.com/jquery-git.js">
    </script>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />

    <style>
      body {
        text-align: center;
      }
      #nonSelected {
        position: absolute;
        top: 150px;
        left: 350px;
        width: 250px;
        height: 280px;
        background-color: lightblue;
        border-width: 2px;
        border-style: dotted;
        border-color: black;
        padding: 10px;
      }

      #selected {
        position: absolute;
        top: 150px;
        left: 20px;
        width: 250px;
        height: 280px;
        background-color: lightgreen;
        font-style: italic;
        border-width: 2px;
        border-style: solid;
        border-color: black;
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <h2 style="color: green">GeeksforGeeks</h2>
    <b>Make groups of three colors.</b>

    <div id="nonSelected">
      <div id="div1" onclick="move(this)" 
           style="background-color: green">
        Inner division one
      </div>
      <div id="div2" onclick="move(this)" 
           style="background-color: blue">
        Inner division two
      </div>
      <div id="div3" onclick="move(this)" 
           style="background-color: red">
        Inner division three
      </div>
      <div id="div1" onclick="move(this)" 
           style="background-color: green">
        Inner division one
      </div>
      <div id="div2" onclick="move(this)" 
           style="background-color: blue">
        Inner division two
      </div>
      <div id="div3" onclick="move(this)" 
           style="background-color: red">
        Inner division three
      </div>
      <div id="div1" onclick="move(this)" 
           style="background-color: green">
        Inner division one
      </div>
      <div id="div2" onclick="move(this)" 
           style="background-color: blue">
        Inner division two
      </div>
      <div id="div3" onclick="move(this)" 
           style="background-color: red">
        Inner division three
      </div>
      <div id="div1" onclick="move(this)" 
           style="background-color: green">
        Inner division one
      </div>
      <div id="div2" onclick="move(this)" 
           style="background-color: blue">
        Inner division two
      </div>
      <div id="div3" onclick="move(this)" 
           style="background-color: red">
        Inner division three
      </div>
      <div id="div1" onclick="move(this)" 
           style="background-color: green">
        Inner division one
      </div>
      <div id="div2" onclick="move(this)" 
           style="background-color: blue">
        Inner division two
      </div>
      <div id="div3" onclick="move(this)" 
           style="background-color: red">
        Inner division three
      </div>
    </div>
    <div id="selected"></div>
    <script>
      function move(elem) {
        if ($(elem).parent().attr("id") == "nonSelected") {
          $(elem).detach().appendTo("#selected");
        } else {
          $(elem).detach().appendTo("#nonSelected");
        }
      }
    </script>
  </body>
</html>

Output:



Similar Reads

How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
How to clear all div's content inside a parent div ?
In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the
2 min read
jQuery | Move an element into another element
There are two methods to move an element inside another element are listed below: Method 1: Using append() method: This append() Method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) ) Parameters: This method accepts two parameters as mentioned above and descr
2 min read
How to center a div on the edge of another div in SASS ?
In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. Centering a div on the edge of another div is not a difficult task. We can achieve it b
4 min read
How to Center a Div within another Div?
We are given a HTML div inside another div tag and the task is to center the inside div. The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed. Syntax:<div> <h3>'This is a div of the web page.>/h3> <p>This is some text in a div element.</p></div> Exam
4 min read
How to Wrap an Element with Another Div Using jQuery?
We will see how to wrap an element with another div in jQuery. We will simply wrap an element inside a div tag in jQuery. These are the following approaches: Table of Content Using wrap() method Using wrap.inner() methodApproach 1: Using wrap() method We include the jQuery library in the <head> section.We have a <div> with the ID elemen
3 min read
How to move a container inside of another container in Bootstrap 5 ?
The container is used to set the content’s margin. It contains row elements and the row elements are containers of columns. This is known as the grid system. In this article, we will learn about How to move a container inside of another container in Bootstrap 5. We will move a container to the left in another container. Syntaxms-0 me-0 Table of Con
2 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to move an array element from one array position to another in JavaScript?
In JavaScript, we can access an array element as in other programming languages like C, C++, Java, etc. Also, there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another we can splice() method or we can simply use array
5 min read
How to Scroll to an Element Inside a Div using JavaScript?
To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript: Table of Content Using scrollIntoView( ) method Using scroll( ) method Using scrollT
3 min read
How to Move MongoDB Document from one Collections to Another ?
Mongoose is an essential ODM (Object Data Modeling) library for MongoDB designed to work seamlessly with Node.js. It provides a structured way to define schemas, making it easier to interact with MongoDB collections. In this guide, we will cover the installation of Mongoose, the setup of schemas for source and destination collections and the implem
3 min read
How to Change Background Color of a Div on Mouse Move Over using JavaScript ?
The background color of the div box can be easily changed using HTML, CSS, and Javascript. We will use the querySelector() and addEventListener() methods to select the element and then apply some math logic to change its background color. The below sections will guide you on how to create the effect. HTML Code: In this section, we will create a bas
2 min read
How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS ?
We will know how to center-align the image in a div tag using CSS & will also understand its implementation through the example. Given an image, we need to set the image that align to the center (vertically and horizontally) inside a bigger div. But first let's create a basic structure, in which inside the body tag with id=outer there is an img
2 min read
How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
How to make div fixed after scroll to that div ?
In this article, we will see how to make a div fixed after you scroll to it by using the position property with a sticky value. This effect is generally used on websites when an important message needs to be shown. Set the property position to sticky to create elements that behave as fixed positions whenever the user scrolls to that position. Fixin
4 min read
How to Overlay/Hover Div in Flexbox Container Div?
In CSS, overlay/hover effects can be achieved by using Flexbox containers to center content and applying styles to reveal overlays on hover. By positioning elements absolutely within a Flexbox container, overlays can cover the entire content area. These are the following approaches: Table of Content Using ::before Pseudo-elementUsing an Overlay Div
3 min read
How to show/hide div element depending multiple values using Bootstrap and jQuery ?
Bootstrap and jQuery have been used together to develop interactive web applications. Both Bootstrap and jQuery are free and open source which makes it popular among developers. This article deals with showing and hiding division depending on user input with the help of Bootstrap and jQuery. Below shown are two approaches to establish the same. Fir
5 min read
How to retrieve stored value from div element using jQuery ?
In this article, we will learn how to retrieve the stored value from any division element using jQuery. This can be used in situations where the value of the division element has to be manipulated or used as per requirements. Approach 1: We will be using the jQuery data() ,first(), last() and text() methods. We attach or store the data of the divis
3 min read
Create a Div Element using jQuery
In this article, we will see how to create a <div> element using jQuery. We can create a div element with the following steps: Steps to Create a Div ElementCreate a new <div> element.Choose a parent element, where to put this newly created element.Put the created div element into the parent element.Example 1: This example creates a <
2 min read
How to apply CSS in a particular div element using jQuery ?
In this article, we will set the CSS of a div element using jQuery. We will use the CSS property to set the CSS. Approach: We will create a div element with some text and hyperlinks. Then we will use jQuery to apply CSS to the div element. jQuery comes with the function .css() where we can get and set CSS of any element. We will use it for the div
2 min read
How to dynamically set the height and width of a div element using jQuery ?
Set the height of a div element using jQueryThe content height of a div can be dynamically set or changed using height(), innerHeight(), and outerHeight() methods depending upon the user requirement. If the user wants to change the content height of a div dynamically, it includes changing the actual height, actual height with padding, and actual he
7 min read
How to place a div inside an iframe for IE ?
Introduction: An Iframe is used to display a web page (or possibly a document) within a web page, i.e. it loads the contents of a page (or document) inside the current page. Iframe Syntax: <iframe src="URL"></iframe> Approach 1: For adding additional div's in an iframe, you need to use a wrapper div, that wraps the contents of your inte
2 min read
How to get elements of specific class inside a div ?
In this article, we will find how to get all elements of a specific class inside an HTML div tag. To do this we will use HTML DOM querySelectorAll()method. This method of HTML DOM (document object model) returns all elements in the document that matches a specified CSS selector(s). The syntax to use this method is as follows. Syntax: document.query
1 min read
How to Place Border Inside Div and Not on its Edge in CSS ?
CSS(Cascading Style Sheet) is a language that provides a style and describes the presentation of a website written in HTML. It provides different layouts, and the appearance of a page, including text, images, and other animation controls. It separates the content and structure of a web page from its presentation, allowing designers to change the ap
3 min read
Image inside div has extra space below the image in CSS
Sometimes when you insert an image inside a div element, you might notice that there is extra space below the image. This can happen due to various reasons such as the image having a different aspect ratio than the div, or the div having padding or margin that affects the image placement. In this article, we will discuss different approaches to sol
5 min read
How to use the $index inside a ng-repeat to enable a class and show a DIV in AngularJS ?
In AngularJS applications, we can use the $index variable within an ng-repeat directive to enable a class and show a DIV element based on the index of the current item in the iteration. The $index variable represents the current index of the item being processed in the ng-repeat loop. Similarly, to show or hide a DIV element, you can use the ng-sho
6 min read
How to Vertically Align an Image Inside a Div in CSS ?
In this article, we are going to see how we can vertically align an image inside a div. Centering content both vertically and horizontally is one of the most important functions which needs to be done. Below are the approaches to vertically align an image inside a div in CSS:   Table of Content Using CSS Display propertyUsing CSS Position Propertie
4 min read
How to perform click-and-hold operation inside an element using jQuery ?
Given an HTML element and the task is to click and hold an element in a document for a few seconds to perform the operation using jQuery. Approach: Select an HTML element. Add the event listener to that element and add a timeOut timer for that event. If that event happens to active for few seconds then trigger other event to identify that the event
2 min read