JavaScript Drag and drop and drag again using pure javascript

I am attempting to do a simple drag and drop from one column to another. I am copying the element so that the list on the right can have multiple versions of the element on the left. Don't worry, I'm setting unique IDs before the actual append.

However I also want the user to be able to drag out of the box to delete that same object. But once the DIV is dropped into place (i.e. once it's in column2), it cannot be dragged again. The initial drag and drop works fine.

Solutions I've found deal with jQuery ui. I'm building an angularJS app and am not interesting in using full jQuery nor any additional plug-ins.

Help!

Sample code:

<div id="column1">    
    <div class="dragme" draggable="true" ondragstart="drag(event)">Item1</div>   
    <div class="dragme" draggable="true" ondragstart="drag(event)">Item1</div>
</div>

<div id="column2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<script>
function allowDrop(ev) {
    ev.preventDefault();
}
function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    var newEl = $(document.getElementById(data)).clone()[0];
    newEl.id = newEl.id + (+new Date());
    ev.target.appendChild(newEl);
}
</script>

UPDATE

It seems that it's not the dragging and dropping that's causing the issue. Any dynamically added content won't drag. I tested this in the console.

FYI...The code below works just fine here, the problem ended up being that my drag/drop was within a draggable container.

function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
  var origThing = document.getElementById(data);
  var newThing = origThing.cloneNode(true);
    ev.target.appendChild(newThing);
}
.thing {
  width: 100px;
  height: 2em;
  padding: 0.5em;
  margin: 0.5em;
  background: rgba(0,0,0,0.8);
  color: white;
  font-family: sans-serif;
 }
.col {
  width: 130px;
  height: 450px;
  padding: 1em;
  border: 1px solid;
  border-radius: 5px;
  position: relative;
  float: left;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="col" id="col1">
  <div class="thing" draggable="true" ondragstart="drag(event)" id="thing1">THING 1</div>
  <div class="thing" draggable="true" ondragstart="drag(event)" id="thing2">THING 2</div>  
  <div class="thing" draggable="true" ondragstart="drag(event)" id="thing3">THING 3</div>  
  <div class="thing" draggable="true" ondragstart="drag(event)" id="thing4">THING 4</div>
</div>
<div class="col" id="col2" ondrop="drop(event)" ondragover="allowDrop(event)">

</div>
Answer:1

Here's a pure JavaScript way to drag dynamically created divs around the window.

var divs=[];
var draggingIndex;
var isDown=false;
var startX,startY;

// add some divs dynamically
addDiv(50,50,100,75,'blue','batch1');
addDiv(250,50,50,38,'green','batch1');

// listen to mouse events
window.onmousedown=(function(e){handleMouseDown(e);});
window.onmousemove=(function(e){handleMouseMove(e);});
window.onmouseup=(function(e){handleMouseUp(e);});


function addDiv(x,y,w,h,bk,classname){
    var div=document.createElement('div');
    div.style.width=w+'px';
    div.style.height=h+'px';
    div.className=classname;
    div.style.position='absolute';
    div.style.left=x+'px';
    div.style.top=y+'px';
    div.style.background=bk;
    divs.push({div:div,w:w,h:h});
    document.body.appendChild(div);
}


function handleMouseDown(e){
    // tell the browser we're handling this event
    e.preventDefault();
    e.stopPropagation();
    // get mouse position
    startX=parseInt(e.clientX);
    startY=parseInt(e.clientY);
    // Is any div under the mouse?
    draggingIndex=undefined;
    for(var i=0;i<divs.length;i++){
        var d=divs[i];
        var x=parseInt(d.div.style.left);
        var y=parseInt(d.div.style.top);
        if(startX>x && startX<x+d.w && startY>y && startY<y+d.h){
            draggingIndex=i;
            isDown=true;
            break;
        }
    }
}

function handleMouseUp(e){
    // tell the browser we're handling this event
    e.preventDefault();
    e.stopPropagation();
    isDown=false;
}

function handleMouseMove(e){
    if(!isDown){return;}
    // tell the browser we're handling this event
    e.preventDefault();
    e.stopPropagation();
    // get mouse position
    mouseX=parseInt(e.clientX);
    mouseY=parseInt(e.clientY);
    // move the dragging div by the distance the mouse has moved
    var dragging=divs[draggingIndex].div;
    var dx=mouseX-startX;
    var dy=mouseY-startY;
    startX=mouseX;
    startY=mouseY;
    dragging.style.left=(parseInt(dragging.style.left)+dx)+'px';
    dragging.style.top=(parseInt(dragging.style.top)+dy)+'px';
}
body{ background-color: ivory; }
.batch1{border:1px solid red; }
Answer:2

I have a ul list which has li's and in li there is div with no class and id. I just want to find li>div which has text saying 'Certificats et forfaits' and hide anchor tag which is next to it. Is it ...

I have a ul list which has li's and in li there is div with no class and id. I just want to find li>div which has text saying 'Certificats et forfaits' and hide anchor tag which is next to it. Is it ...

  1. find specific string in excel
  2. find specific string in python
  3. find specific string in list python
  4. find specific string in php
  5. find specific string regex
  6. find specific string in linux
  7. find specific string in excel cell
  8. find specific string matlab
  9. find specific string in file python
  10. find specific string in c#
  11. find specific string in file linux
  12. find specific string in array javascript
  13. find specific string in sql
  14. find specific string in text file powershell
  15. find specific string in all files linux
  16. find specific string in javascript
  17. find specific string in stored procedure
  18. find specific string in sql database
  19. find specific string in r
  20. find specific string in jquery

The controller I'm using is: angular.module('app.PostView', []) .controller('PostViewCtrl', function($scope, $http, Constants) { $scope.posts = []; $scope.doSomething = function(){ ...

The controller I'm using is: angular.module('app.PostView', []) .controller('PostViewCtrl', function($scope, $http, Constants) { $scope.posts = []; $scope.doSomething = function(){ ...

I want to delete "()" from each value. How would I do that? var arr = ["(one)","(two)","(three)","(four)","(five)"]; for(var i = 0; i < arr.length; i++){ console.log(arr[i]); }

I want to delete "()" from each value. How would I do that? var arr = ["(one)","(two)","(three)","(four)","(five)"]; for(var i = 0; i < arr.length; i++){ console.log(arr[i]); }

  1. javascript trim characters from end
  2. javascript trim character from start
  3. javascript trim characters from beginning of string
  4. javascript trim character from start and end
  5. javascript trim character length
  6. javascript trim character limit
  7. javascript trim last character
  8. javascript trim first character
  9. javascript trim specific character
  10. javascript trim after character
  11. javascript trim right character
  12. javascript trim special characters
  13. javascript trim last character if comma
  14. javascript trim last character of string
  15. javascript trim first character from string
  16. javascript trim custom characters
  17. javascript trim other characters
  18. javascript trim last character if slash

I have been trying to parse nested JSON data and below is my code var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}'; var obj = JSON.parse(string); console.log(obj.key1) ...

I have been trying to parse nested JSON data and below is my code var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}'; var obj = JSON.parse(string); console.log(obj.key1) ...

  1. parsing nested json in python
  2. parsing nested json in javascript
  3. parsing nested json in java
  4. parsing nested json data in javascript
  5. parsing nested json c#
  6. parsing nested json swift
  7. parsing nested json golang
  8. parsing nested json in vba
  9. parsing nested json in spark
  10. parsing nested json in node js
  11. parsing nested json in flutter
  12. parsing nested json array in java
  13. parsing nested json using jackson
  14. parsing nested json with pandas
  15. parsing nested json in pyspark
  16. parsing nested json logstash
  17. parsing nested json in java using jackson
  18. parsing nested json objects in python
  19. parsing nested json in swift 4