Skip to content

Instantly share code, notes, and snippets.

@yuvalbl
Last active October 19, 2020 08:55
Show Gist options
  • Save yuvalbl/5ee62f14b486dcc3d726b4a469ad3e03 to your computer and use it in GitHub Desktop.
Save yuvalbl/5ee62f14b486dcc3d726b4a469ad3e03 to your computer and use it in GitHub Desktop.
Text drag and drop
<div ondragover="allowDrop(event)">
Drop here
</div>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
</script>
<div ondragover="allowDrop(event)" ondrop="drop(event)">
Drop here
</div>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drop(ev) {
const text = ev.dataTransfer.getData('text');
document.getElementById('drop').innerHTML = text
}
</script>
<div ondragover="allowDrop(event)" ondrop="drop(event)">
Drop here
</div>
<div id="text1" ondragstart="drag(event)">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<script>
//...
function drag(ev) {
const selection = document.getSelection();
const containerId = selection.anchorNode.parentElement.id;
ev.dataTransfer.setData('containerId', containerId);
}
</script>
<div ondragover="allowDrop(event)" ondrop="drop(event)">
Drop here
</div>
<div id="text1" ondragstart="drag(event)">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<script>
//...
function drop(ev) {
const containerId = ev.dataTransfer.getData('containerId')
const text = ev.dataTransfer.getData('text')
const content = `<b>[Container Id: ${containerId}]</b> ${text}`
document.getElementById('drop').innerHTML = content;
}
</script>
<!DOCTYPE HTML>
<html>
<head>
<style>
div {
border: 1px dashed gray;
padding: 20px;
margin: 20px;
}
p {
padding: 0 20px;
}
.drop-zone {
border: 2px solid black
}
</style>
</head>
<body>
<div id="drop" class="drop-zone" ondrop="drop(event)" ondragover="allowDrop(event)">
Drop here
</div>
<div id="text1" ondragstart="drag(event)">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.</div>
<div id="text2" ondragstart="drag(event)">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</div>
<script>
'use strict';
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
const selection = document.getSelection();
const containerId = selection.anchorNode.parentElement.id;
ev.dataTransfer.setData('containerId', containerId);
}
function drop(ev) {
const containerId = ev.dataTransfer.getData('containerId')
const text = ev.dataTransfer.getData('text')
const content = `<b>[Container Id: ${containerId}]</b> ${text}`
document.getElementById('drop').innerHTML = content;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment