Easily move a div by dragging and dropping it from one location to another

Looking to seamlessly transfer a div from one container to another.

I've got the CSS and HTML code ready for you to assist me, here is the snippet:

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
      #myDIV {
        border: 1px solid black;
        margin-bottom: 10px;
        width: 500px;
        height: 300px;
        box-sizing: border-box;
        border: solid #000 2px;
        background-color: #d12;
      }

      .ZE {
        position: fixed;
        width: 400px;
        height: 100px;
        background-color:  #73AD81;
        overflow: hidden;
        border-radius: 20px 60px;
        border: 2px solid #965D31;
      }

      .ZE.left {
        top: 50%;
        transform: translateY(-50%);
      }

      .ZE.right  {
        right: 0;
        top: 50%;
        transform: translateY(-50%);
      }

      .ZE.up {
        left: 50%;
        transform: translateX(-50%);
      }

      .ZE.down {
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
      }
    </style>
  </head>
  <body>
    <div id="parent-div">
      <div class="myDIV" id="myDIV"></div>
    </div>
    <script type="text/javascript">
      var parent = document.getElementById("parent-div");

      // for demo purposes
      var data = {
        isConnected: true
      },
          classes = ['left', 'right', 'up', 'down'];

      //socket.on("isConnected", function(data) {
       Receive the 'data' and confirm 'isConnected' is set to true

      // loop for demo purposes
      for (var i = 0; i < 4; i++) {

        if (data.isConnected === true) {
          parent.innerHTML += "<div class='ZE " + classes[i] + "'></div>"
        }

      }
      //});
    </script>
  </body>
</html>

Seeking guidance on moving the #myDIV div into the other generated divs such as ZE left ZE right. I believe it should be straightforward, but being new to this, some assistance would be appreciated.

Answer №1

I encountered a similar question in a previous post (where I posed the question). By examining the JSFiddle code located in the comments section and the provided answer, you should be able to grasp the solution. It took me some time to tailor it to fit my requirements, but I eventually succeeded. So, persevere and continue experimenting with that code. Don't forget to add the JQuery library reference in your HTML's section like this:

<head>
   <!--JQuery Library for implementing answer steps-->
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>

I hope this guidance sets you on the right path.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adapt the dimensions of the iframe to perfectly match the content within

Looking for a way to dynamically adjust the size of an iframe to perfectly fit its content, even after the initial load. It seems like I'll need some kind of event handling to automatically adjust the dimensions based on changes in the content within ...

What is the Typescript definition of a module that acts as a function and includes namespaces?

I'm currently working on creating a *.d.ts file for the react-grid-layout library. The library's index.js file reveals that it exports a function - ReactGridLayout, which is a subclass of React.Component: // react-grid-layout/index.js module.exp ...

Data of an object disappears when it is passed to Meteor.call

In my React/Meteor project, I encountered an issue while trying to pass an object with data from the state to a method on the server for database insertion. Surprisingly, when the object is passed from the React component to the Meteor method, one of the c ...

Having trouble accessing Kotlin JS from JavaScript

I'm currently experiencing an issue where I am unable to call any Kotlin JS function and receiving an error stating that 'something' is not defined. Initially, I attempted compiling the project with Gradle but found success after following ...

"Embrace the power of Ajax and JSON with no regrets

function register() { hideshow('loading', 1); //error(0); $.ajax({ type: 'POST', dataType: 'json', url: 'submit.php', data: $('#regForm').serialize(), su ...

The state is not being configured accurately

In my ReactJs project, I have a model Component with a picture that is displayed. I want to pass the data of the clicked picture, which can be neither raw data nor a URL. I have implemented a handler that can delete the picture (if pressed with the Ctrl k ...

Move values between divs using drag and drop, then execute MySQL queries once the values have been transferred

I am embarking on my first project involving drag and drop functionality. I need to create a timetable application for a college that allows users to drag teacher responsibilities from a list on the left side and drop them into a timetable slot on the ri ...

Leveraging React Native to retrieve information from a promise in JSON format

After successfully fetching the JSON data, I now have an array. My question is how can I utilize the elements within this array in a React Native environment? Below is my current approach: export default function display() { const fetching = async() => ...

After being redrawn, the line on the canvas vanishes

After updating the angle value, the line is quickly redrawn correctly but then disappears. It seems like the input value may be getting refreshed and set to undefined again. How can I ensure that the line retains the correct angle? <script language=" ...

The functionality of $http get parameters is malfunctioning

Is there a reason why this code snippet is not functioning properly? $http .get('accept.php', { source: link, category_id: category }) .success(function (data, status) { $scope.info_show = data }); On the ...

Javascript/Typescript Performance Evaluation

I am looking to create a visual report in the form of a table that displays the count of each rating based on the date. The ratings are based on a scale of 1 to 5. Below is the object containing the data: [ { "Date": "01/11/2022", ...

angular-bootstrap-mdindex.ts is not included in the compilation result

Upon deciding to incorporate Angular-Bootstrap into my project, I embarked on a quest to find a tutorial that would guide me through the download, installation, and setup process on my trusty Visual Studio Code. After some searching, I stumbled upon this h ...

The 'npm start' command is failing to recognize the changes made to the

I am embarking on a new node application journey, starting with the package.json below, which is quite basic. { "name": "mynewnodeventure", "version": "1.0.1", "description": "Exploring node", "main": "index.js", "start": "node server. ...

avoidable constructor in a react component

When it comes to specifying initial state in a class, I've noticed different approaches being used by people. class App extends React.Component { constructor() { super(); this.state = { user: [] } } render() { return <p>Hi</p> ...

How come I am unable to access a local JSON file using AngularJS $http?

I've been attempting to load a local file (todo.json) located in the same directory as my webpage using the following line of code: $http.get("todo.json").success( function( data ){ //Do Some logic}); However, when I try to do so, I encounter the fo ...

C# variable value not being retrieved by jQuery

When I implemented the jQuery scripts in my .aspx file, everything worked perfectly. Here is the code snippet: $(function() { var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C+ ...

Sending a form cancellation submission within a nested function call

I need help preventing my form from submitting in case the confirmation message is cancelled. How can I achieve this within the each() loop? $('#myForm').submit(function() { var inputs = $(this).find('input:checked'); inputs.ea ...

Creating a Rectangular Trapezoid Shape with CSS - Eliminating Unnecessary Spacing

I'm trying to create a trapezoid button using CSS. Here is the intended look: https://i.sstatic.net/0vqlk.png However, my current implementation looks like this: https://i.sstatic.net/QrR4t.png The button appears fine but there seems to be some e ...

What is preventing me from altering the array one element at a time?

I am working with an array and a class let questions = [ { questionText: '', answerOptions: [], }, ]; class Questions { constructor(questionText,answerOptions) { this.questionText = questionText; this.answerOptio ...

Navigating between applications

Does anyone have suggestions on setting up routing between different apps without disrupting existing code? We want to make the integration process easy when adding a new app to our main application. For example, navigating from an electronics(main app) ...