-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (44 loc) · 1.54 KB
/
script.js
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
//Elements
const getText = document.querySelector(".textBox");
const droppableElements = document.querySelectorAll(".section");
const draggableElements = document.querySelectorAll(".swap");
const todoSection = document.getElementById("todo");
const addButton = document.querySelector(".addBtn");
//EventListener to create new Draggables
addButton.addEventListener("click", function (e) {
e.preventDefault();
let values = getText.value;
if (values === "") {
alert("Please, Enter a some content to add../");
} else {
const newDraggable = document.createElement("p");
newDraggable.classList.add("swap");
newDraggable.setAttribute("draggable", "true");
newDraggable.innerText = values;
getText.value = "";
newDraggable.addEventListener("dragstart", function () {
newDraggable.classList.add("is-dragging");
});
newDraggable.addEventListener("dragend", function () {
newDraggable.classList.remove("is-dragging");
});
todoSection.appendChild(newDraggable);
}
});
//Events to each draggables
draggableElements.forEach((drag) => {
drag.addEventListener("dragstart", function () {
drag.classList.add("is-dragging");
});
drag.addEventListener("dragend", function () {
drag.classList.remove("is-dragging");
});
});
//Event to each droppables
droppableElements.forEach((drop) => {
drop.addEventListener("dragover", (e) => {
e.preventDefault();
let currentDrag = document.querySelector(".is-dragging");
drop.appendChild(currentDrag);
});
});