MCPcopy Create free account
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / drop

Function drop

assets/js/DragNDrop.js:45–60  ·  view source on GitHub ↗
(event)

Source from the content-addressed store, hash-verified

43}
44
45function drop(event) {
46 event.preventDefault(); // This is in order to prevent the browser default handling of the data
47 event.target.classList.remove("droppable-hover");
48 const draggableElementData = event.dataTransfer.getData("text"); // Get the dragged data. This method will return any data that was set to the same type in the setData() method
49 const droppableElementData = event.target.getAttribute("data-draggable-id");
50 const isCorrectMatching = draggableElementData === droppableElementData;
51 if (isCorrectMatching) {
52 const draggableElement = document.getElementById(draggableElementData);
53 event.target.classList.add("dropped");
54 // event.target.style.backgroundColor = draggableElement.style.color; // This approach works only for inline styles. A more general approach would be the following:
55 event.target.style.backgroundColor = window.getComputedStyle(draggableElement).color;
56 draggableElement.classList.add("dragged");
57 draggableElement.setAttribute("draggable", "false");
58 event.target.insertAdjacentHTML("afterbegin", `<i class="fas fa-${draggableElementData}"></i>`);
59 }
60}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected