Drag and Drop Change Background Color in JavaScript

Drag and Drop Change Background Color in JavaScript


In this tutorial we will create a Drag and Drop Change Background Color in JavaScript. This tutorial purpose is to teach how to use the drag and drop feature. This will cover all the basic functions that will make this program works. 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 kind of system that needed to implement a drag and drop function. I will give my best to provide you the easiest way of creating this program Drag and Drop. 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 only a box and several colored boxes. 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">Drag and Drop Change Background Color</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <div class="col-md-12">
  18. <div style="width:50px; height:50px; border:1px solid red; background-color:red;" id="red" draggable="true" ondragstart="drag(event)"></div>
  19. <br />
  20. <div style="width:50px; height:50px; border:1px solid yellow; background-color:yellow;" id="yellow" draggable="true" ondragstart="drag(event)"></div>
  21. <br />
  22. <div style="width:50px; height:50px; border:1px solid blue; background-color:blue;" id="blue" draggable="true" ondragstart="drag(event)"></div>
  23. <br />
  24. <div style="width:50px; height:50px; border:1px solid green; background-color:green;" id="green" draggable="true" ondragstart="drag(event)"></div>
  25. <br />
  26. <div style="width:50px; height:50px; border:1px solid black; background-color:black;" id="black" draggable="true" ondragstart="drag(event)"></div>
  27. </div>
  28. </div>
  29. <div class="col-md-6" style="border:1px solid #000; height:200px; padding-top:10%; padding-left:10%;" ondrop="drop(event)" ondragover="dragOver(event)">
  30. <h2>DRAG HERE</h2>
  31. </div>
  33. </div>
  34. <script src="script.js"></script>
  35. </body>
  36. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will let you dynamically change a background color. 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 data = e.dataTransfer.getData("data");
  4. e.target.style.backgroundColor = data;
  5. e.target.innerHTML = "";
  7. }
  9. function drag(e){
  10. e.dataTransfer.setData("data", e.target.id);
  11. }
  13. function dragOver(e){
  14. e.preventDefault();
  15. }

In this code we will create first a method that will detect the appended object called drop()function, this will detect the dropped object and will get the data id. After we getting the object id we will apply the data as the background color of the targeted element. Next we will create a method called drag(), this method will set an object data that will will be carry over while dragging. Lastly we will create a method called called dragOver() function, this will allow the object to be draggable.


The Drag and Drop Change Background Color in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created Drag and Drop Change Background Color 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