How to Transfer your Image to Different Div in JavaScript

How to Transfer your Image to Different Div in JavaScript

Introduction

In this tutorial we will create a How to Transfer your Image to Different Div in JavaScript. This tutorial purpose is to teach you to how you can move the image from div to div. This will cover all the basic function that will drag image. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that uses drag and drop feature. I will give my best to provide you the easiest way of creating this program Move Image from div to div. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html divs and image. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Transfer your Image to Different Div in JavaScript</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <h5>Drag Image to Move</h5>
  17.                 <div class="col-md-5" style="border:1px SOLID #ccc; height:120px;" ondrop="drop(event)" ondragover="dragOver(event)"><h4 style="position:absolute;">DIV 1</h4></div>
  18.                 <div class="col-md-1"></div>
  19.                 <div class="col-md-5" style="border:1px SOLID #ccc; height:120px;" ondrop="drop(event)" ondragover="dragOver(event)"><h4 style="position:absolute;">DIV 2</h4></div>
  20.                 <br style="clear:both;"/><br />
  21.                 <div class="col-md-5" style="border:1px SOLID #ccc; height:120px;" ondrop="drop(event)" ondragover="dragOver(event)"><h4 style="position:absolute;">DIV 3</h4></div>
  22.                 <div class="col-md-1"></div>
  23.                 <div class="col-md-5" style="border:1px SOLID #ccc; height:120px;" ondrop="drop(event)" ondragover="dragOver(event)"><h4 style="position:absolute;">DIV 4</h4></div>
  24.                 <br style="clear:both;"/><br />
  25.                 <center><img src="image.png" id="image" height="100" width="150" draggable="true" ondragstart="drag(event)"/></center>
  26.         </div>
  27. <script src="script.js"></script>      
  28. </body>
  29. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow move your image from div to another div. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function drop(e){
  2.         e.preventDefault();
  3.         let image = e.dataTransfer.getData("image");
  4.         e.target.appendChild(document.getElementById(image));
  5.         document.getElementById(image).style="width:100%;";
  6. }
  7.  
  8.  
  9. function dragOver(e){
  10.         e.preventDefault();
  11. }
  12.        
  13. function drag(e){
  14.         e.dataTransfer.setData("image", e.target.id);
  15. }      
  16.        

In this code we just used the common drag and drop features. We just create three method namely drop, dragOver, drag. In the dragfunction we just set the image data in a transferable variable. Next we use the dragOver function to prevent the default function of the drag and drop. Lastly in the drop function we just receive the image data from the dragging event and set it into the targeted div as a background image.

Output:

The How to Transfer your Image to Different Div in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Transfer your Image to Different Div in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment