Skip to main content

JavaScript - Sum Total Object When Drag

In this tutorial we will create a Sum Total Object When Drag using JavaScript. This code will count all the objects when the user drag it to the target area. The code use ondrop() method to catch and get the object value base on the binded id in order to sum up the total object that has been drag. This is a free program, you can modify it and use it as your own.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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="">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">JavaScipt - Sum Total Object When Drag</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <div style="background-color:blue; margin-left:15px; width:75px; height:75px; float:left;" id="blue" draggable="true" ondragstart="drag(event)" ></div>
  18. <div style="background-color:yellow; margin-left:15px; width:75px; height:75px; float:left;" id="yellow" draggable="true" ondragstart="drag(event)"></div>
  19. <div style="background-color:red; margin-left:15px; width:75px; height:75px; float:left;" id="red" draggable="true" ondragstart="drag(event)"></div>
  20. <br style="clear:both;"/><br />
  21. <center><div style="width:100px; height:100px; border:1px solid #000;" ondrop="drop(event)" ondragover="dragOver(event)"></div></center>
  22. </div>
  23. <div class="col-md-2"></div>
  24. <div class="col-md-4">
  25. <table class="table table-bordered">
  26. <tr>
  27. <td>Blue</td>
  28. <td><label id="blue_counter">0</label></td>
  29. </tr>
  30. <tr>
  31. <td>Yellow</td>
  32. <td><label id="yellow_counter">0</label></td>
  33. </tr>
  34. <tr>
  35. <td>Red</td>
  36. <td><label id="red_counter">0</label></td>
  37. </tr>
  38. </table>
  39. </div>
  40. </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will sum the total object when the object is dragged. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. var blue = 0;
  2. var yellow = 0;
  3. var red = 0;
  6. function dragOver(e){
  7. e.preventDefault();
  8. }
  10. function drop(e){
  11. e.preventDefault();
  12. var data = e.dataTransfer.getData("data");
  14. switch(data){
  15. case "blue":
  16. blue++;
  17. document.getElementById('blue_counter').innerHTML = blue;
  18. break;
  19. case "yellow":
  20. yellow++;
  21. document.getElementById('yellow_counter').innerHTML = yellow;
  22. break;
  23. case "red":
  24. red++;
  25. document.getElementById('red_counter').innerHTML = red;
  26. break;
  27. }
  29. alert("You have drop a box!");
  30. }
  32. function drag(e){
  33. e.dataTransfer.setData("data",;
  34. }

There you have it we successfully created a Sum Total Object When Drag using 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.