Skip to content

Instantly share code, notes, and snippets.

@9jaswag
Created May 16, 2018 10:37
Show Gist options
  • Save 9jaswag/2f7e8546ac7ab5b315b6c1e324a7e813 to your computer and use it in GitHub Desktop.
Save 9jaswag/2f7e8546ac7ab5b315b6c1e324a7e813 to your computer and use it in GitHub Desktop.
drag and drop js
const dragStart = (event) => {
event.dataTransfer.setData("text/plain", event.target.id);
}
const allowDrop = (event) => {
event.preventDefault();
event.currentTarget.style.background = '#7f8082';
}
const drop = (event) => {
event.preventDefault();
const data = event.dataTransfer.getData("text/plain");
const element = document.querySelector(`#${data}`);
event.currentTarget.style.background = 'white'
try {
event.target.appendChild(element);
} catch (error) {
console.warn("you can't move the item to the same place")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment