HTML5 Drag and Drop Feature
Code Snippet for Drag and Drop Image
<!DOCTYPE html>
<html>
<title></title>
<!--Internal CSS To declare box out line-->
<style>
.box,.box2{
width:300px;
height:300px;
border:solid 2px red;
}
.box2{
margin-top: 5px;
}
</style>
<!--declaration ends-->
<h1></h1>
<body>
<!--delcaring box1 class-->
<div class="box" >
<!-- image to be dragged-->
<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem"> </div>
<div class="box2" id="targetbox"></div>
<!--box2 declared to received dragged image from box1-->
</form>
<script>
document.getElementById("imagedragitem").ondragstart=function(e){
console.log(e);
console.log("calling on drop");
let data='<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem">';
e.dataTransfer.setData("text",data);
}
<!--ondrop event is --> dragged item (element or text selection) is dragged.t-->
document.getElementById("targetbox").ondrop=function(e){
console.log(e);
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
this.innerHTML=fetchedData;
}
<!--ondragenter event is --> a dragged item enters a valid drop target-->
document.getElementById("targetbox").ondragenter=function(e){
console.log("calling on drage enter");
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
e.preventDefault();
document.querySelector(".box").innerHTML="";
}
<!--ondragover event is --> a dragged item is being dragged over a valid drop target, every few hundred milliseconds.-->
document.getElementById("targetbox").ondragover=function(e){
console.log("dragover");
e.preventDefault();
}
</script>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------
Above code can be further improvised for reusability
--------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<title></title>
<!--Internal CSS To declare box out line-->
<style>
.box,.box2{
width:300px;
height:300px;
border:solid 2px red;
}
.box2{
margin-top: 5px;
}
</style>
<!--declaration ends-->
<h1></h1>
<body>
<!--delcaring box1 class-->
<div class="box" >
<!-- image to be dragged-->
<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem"> </div>
<div class="box2" id="targetbox"></div>
<!--box2 declared to received dragged image from box1-->
</form>
<script>
document.getElementById("imagedragitem").ondragstart=ondragstartevt;
document.getElementById("targetbox").ondrop=ondropevt;
document.getElementById("targetbox").ondragenter=ondragenterevt;
document.getElementById("targetbox").ondragover=ondrageoverevet;
function ondragstartevt(e){
console.log(e);
console.log("calling on drop");
let data='<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem">';
e.dataTransfer.setData("text",data);
}
<!--ondrop event is --> dragged item (element or text selection) is dragged.t-->
function ondropevt(e){
console.log(e);
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
this.innerHTML=fetchedData;
}
<!--ondragenter event is --> a dragged item enters a valid drop target-->
function ondragenterevt(e){
console.log("calling on drage enter");
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
e.preventDefault();
document.querySelector(".box").innerHTML="";
}
<!--ondragover event is --> a dragged item is being dragged over a valid drop target, every few hundred milliseconds.-->
function ondrageoverevet(e){
console.log("dragover");
e.preventDefault();
}
</script>
</body>
</html>
Output of above code
________________________________________________________________________________
________________________________________________________________________________
<!DOCTYPE html>
<html>
<title></title>
<!--Internal CSS To declare box out line-->
<style>
.box,.box2{
width:300px;
height:300px;
border:solid 2px red;
}
.box2{
margin-top: 5px;
}
</style>
<!--declaration ends-->
<h1></h1>
<body>
<!--delcaring box1 class-->
<div class="box" >
<!-- image to be dragged-->
<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem"> </div>
<div class="box2" id="targetbox"></div>
<!--box2 declared to received dragged image from box1-->
</form>
<script>
document.getElementById("imagedragitem").ondragstart=function(e){
console.log(e);
console.log("calling on drop");
let data='<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem">';
e.dataTransfer.setData("text",data);
}
<!--ondrop event is --> dragged item (element or text selection) is dragged.t-->
document.getElementById("targetbox").ondrop=function(e){
console.log(e);
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
this.innerHTML=fetchedData;
}
<!--ondragenter event is --> a dragged item enters a valid drop target-->
document.getElementById("targetbox").ondragenter=function(e){
console.log("calling on drage enter");
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
e.preventDefault();
document.querySelector(".box").innerHTML="";
}
<!--ondragover event is --> a dragged item is being dragged over a valid drop target, every few hundred milliseconds.-->
document.getElementById("targetbox").ondragover=function(e){
console.log("dragover");
e.preventDefault();
}
</script>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------
Above code can be further improvised for reusability
--------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<title></title>
<!--Internal CSS To declare box out line-->
<style>
.box,.box2{
width:300px;
height:300px;
border:solid 2px red;
}
.box2{
margin-top: 5px;
}
</style>
<!--declaration ends-->
<h1></h1>
<body>
<!--delcaring box1 class-->
<div class="box" >
<!-- image to be dragged-->
<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem"> </div>
<div class="box2" id="targetbox"></div>
<!--box2 declared to received dragged image from box1-->
</form>
<script>
document.getElementById("imagedragitem").ondragstart=ondragstartevt;
document.getElementById("targetbox").ondrop=ondropevt;
document.getElementById("targetbox").ondragenter=ondragenterevt;
document.getElementById("targetbox").ondragover=ondrageoverevet;
function ondragstartevt(e){
console.log(e);
console.log("calling on drop");
let data='<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="imagedragitem">';
e.dataTransfer.setData("text",data);
}
<!--ondrop event is --> dragged item (element or text selection) is dragged.t-->
function ondropevt(e){
console.log(e);
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
this.innerHTML=fetchedData;
}
<!--ondragenter event is --> a dragged item enters a valid drop target-->
function ondragenterevt(e){
console.log("calling on drage enter");
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData);
e.preventDefault();
document.querySelector(".box").innerHTML="";
}
<!--ondragover event is --> a dragged item is being dragged over a valid drop target, every few hundred milliseconds.-->
function ondrageoverevet(e){
console.log("dragover");
e.preventDefault();
}
</script>
</body>
</html>
Output of above code
________________________________________________________________________________
Same code can be written in other way as well
<!DOCTYPE html>
<html>
<head>
<title>Html5Form</title>
<style>
.box,.box2{
width: 300px;
height:300px;
border:solid 2px red;
}
.box2{
margin-top: 5px;
}
</style>
</head>
<body>
<div class="box">
<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="dragitem" ondragstart="dragStart(event)">
</div>
<div class="box2" id="droptarg" ondrop="dropTarget(event)" ondragenter="dragEnter(event)" ondragover="dragOver(event)"></div>
<script>
function dragStart(e){
console.log(e);
let data='<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="dragitem">';
e.dataTransfer.setData("text",data);
}
function dropTarget(e){
console.log("dropped");
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData)
document.getElementById("droptarg").innerHTML=fetchedData;
document.querySelector('.box').innerHTML="";
}
function dragEnter(e){
console.log("dragenter")
e.preventDefault();
document.querySelector(".box").innerHTML="";
}
function dragOver(e){
console.log("dragover");
e.preventDefault();
}
</script>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>Html5Form</title>
<style>
.box,.box2{
width: 300px;
height:300px;
border:solid 2px red;
}
.box2{
margin-top: 5px;
}
</style>
</head>
<body>
<div class="box">
<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="dragitem">
</div>
<div class="box2" id="droptarg"></div>
<script>
document.getElementById("dragitem").addEventListener("dragstart",dragStart,false);
document.getElementById("droptarg").addEventListener("dragEnter",dragEnter,false);
document.getElementById("droptarg").addEventListener("drop",dropTarget,false);
document.getElementById("droptarg").addEventListener("dragover",dragOver,false);
function dragStart(e){
console.log(e);
let data='<img src="https://i.picsum.photos/id/1025/200/200.jpg" id="dragitem">';
e.dataTransfer.setData("text",data);
}
function dropTarget(e){
console.log("dropped");
let fetchedData=e.dataTransfer.getData("text");
console.log(fetchedData)
this.innerHTML=fetchedData;
document.querySelector('.box').innerHTML="";
}
function dragEnter(e){
console.log("dragenter")
e.preventDefault();
document.querySelector(".box").innerHTML="";
}
function dragOver(e){
console.log("dragover");
e.preventDefault();
}
</script>
</body>
</html>
Read more about drag and drop events from below website
https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
Comments
Post a Comment