How to Create a Drag and Drop Counter in JavaScript

How to Create a Drag and Drop Counter in JavaScript


In this tutorial we will create a How to Create a Drag and Drop Counter in JavaScript. This tutorial purpose is to teach you way to count an object with the use of drag and drop. This will cover all the important function that will drag and drop an object. 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 if you want to add some counter using the drag and drop feature. I will give my best to provide you the easiest way of creating this program Drag and drop counter. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will only display only images and the table data. 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="">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 Create a Drag and Drop Counter</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="row">
  17. <div class="col-md-6" style="">
  18. <center>
  19. <img src="images/image1.png" id="dog" draggable="true" height="70" width="70" ondragstart="drag(event)" />
  20. <img src="images/image2.png" id="elephant" draggable="true" height="70" width="70" ondragstart="drag(event)" />
  21. <img src="images/image3.png" id="lion" draggable="true" height="70" width="70" ondragstart="drag(event)" />
  22. </center>
  23. <center><img src="images/image4.png" style="margin-top:60px;" ondrop="drop(event)" ondragover="dragOver(event)" height="100" width="100"/></center>
  24. </div>
  25. <div class="col-md-5">
  26. <table class="table table-bordered">
  27. <tr>
  28. <td>Dog</td>
  29. <td><label id="dog_count">0</label></td>
  30. </tr>
  31. <tr>
  32. <td>Elephant</td>
  33. <td><label id="elephant_count">0</label></td>
  34. </tr>
  35. <tr>
  36. <td>Lion</td>
  37. <td><label id="lion_count">0</label></td>
  38. </tr>
  39. </table>
  40. </div>
  41. </div>
  42. </div>
  43. <script src="script.js"></script>
  44. </body>
  45. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will count the object that you been dropped in the targeted location To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let dog = 0;
  2. let elephant = 0;
  3. let lion = 0;
  6. function dragOver(e){
  7. e.preventDefault();
  8. }
  10. function drop(e){
  11. e.preventDefault();
  12. let data = e.dataTransfer.getData("data");
  14. switch(data){
  15. case "dog":
  16. dog++;
  17. document.getElementById('dog_count').innerHTML = dog;
  18. break;
  19. case "elephant":
  20. elephant++;
  21. document.getElementById('elephant_count').innerHTML = elephant;
  22. break;
  23. case "lion":
  24. lion++;
  25. document.getElementById('lion_count').innerHTML = lion;
  26. break;
  27. }
  28. }
  30. function drag(e){
  31. e.dataTransfer.setData("data",;
  32. }

In the first line of code we just simply set three global variables that we will use you to count. The first method we will create a method called dragOver(), this will prevent the web browser to default the dragging of an object. Next is the drop() method, this function will receive the object data and use it to increment a counter for each dropped object. And last method is what we call drag() method, this function will carry over the object data while dragging it.


The How to Create a Drag and Drop Counter 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 Create a Drag and Drop Counter 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