-
Notifications
You must be signed in to change notification settings - Fork 1
/
dragdrop.html
64 lines (63 loc) · 1.92 KB
/
dragdrop.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<title>Drag and Drop Elements with Vanilla JavaScript and HTML</title>
<style>
.parent{
display: flex;
}
.left,.right {
width: 200px;
padding: 20px;
border: black 2px solid;
}
.left span{
background-color: blueviolet;
padding: 10px 25px;
display: block;
color: blanchedalmond;
}
.right p {
margin-top: 0;
}
.right span {
display: block;
padding: 10px 25px;
background-color: blueviolet;
color: blanchedalmond;
}
</style>
<script>
function onDragStart(event) {
event.dataTransfer.setData('text/plain', event.target.id);
// event.currentTarget.style.backgroundColor = 'yellow';
}
function onDragOver(event) {
event.preventDefault();
}
function onDrop(event) {
const id = event.dataTransfer.getData('text');
console.log(id);
const draggableElement = document.getElementById(id);
console.log(draggableElement);
const dropzone = event.target;
dropzone.appendChild(draggableElement);
console.log(dropzone);
event.dataTransfer.clearData();
}
</script>
</head>
<body>
<h2>Drag and Drop Elements with Vanilla JavaScript and HTML</h2>
<div class="parent">
<div class="left">
<span id="draggableSpan" draggable="true" ondragstart="onDragStart(event)">
draggable
</span>
</div>
<div class="right" ondragover="onDragOver(event)" ondrop="onDrop(event)">
<p> dropzone </p>
</div>
</div>
</body>
</html>