JavaScript - Simple Count Object By Dragging

In this tutorial we will create a Simple Count Object By Dragging using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

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

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="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">JavaScript - Simple Count Object By Dragging</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-12" style="height:75px;">
  17.                         <img src="images/image1.png" style="margin-left:35px;" id="grape" draggable="true" height="150" width="150" ondragstart="drag(event)" />
  18.                         <img src="images/image2.png" style="margin-left:35px;" id="orange" draggable="true" height="150" width="150" ondragstart="drag(event)" />
  19.                         <img src="images/image3.png" style="margin-left:35px;" id="apple" draggable="true" height="150" width="150" ondragstart="drag(event)" />
  20.                 </div>
  21.                 <br style="clear:both;"/>
  22.                 <center><img src="images/image4.png" style="margin-top:50px;" ondrop="drop(event)" ondragover="dragOver(event)" height="200" width="200"/></center>
  23.                 <div class="col-md-12">
  24.                         <table class="table table-bordered">
  25.                                 <tr>
  26.                                         <td>Grape</td>
  27.                                         <td><label id="grape_count">0</label></td>
  28.                                 </tr>
  29.                                 <tr>
  30.                                         <td>Orange</td>
  31.                                         <td><label id="orange_count">0</label></td>
  32.                                 </tr>
  33.                                 <tr>
  34.                                         <td>Apple</td>
  35.                                         <td><label id="apple_count">0</label></td>
  36.                                 </tr>
  37.                         </table>
  38.                 </div>
  39.         </div>
  40. <script src="js/script.js"></script>   
  41. </body>
  42. </html>

Creating the Script

This code contains the script of the application. This code will count the object by only dragging it to the drop zone. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var grape = 0;
  2. var orange = 0;
  3. var apple = 0;
  4.  
  5.  
  6. function dragOver(e){
  7.                 e.preventDefault();
  8. }
  9.        
  10. function drop(e){
  11.         e.preventDefault();
  12.         var data = e.dataTransfer.getData("data");
  13.        
  14.         switch(data){
  15.                 case "grape":
  16.                         grape++;
  17.                         document.getElementById('grape_count').innerHTML = grape;
  18.                 break;
  19.                 case "orange":
  20.                         orange++;
  21.                         document.getElementById('orange_count').innerHTML = orange;
  22.                 break;
  23.                 case "apple":
  24.                         apple++;
  25.                         document.getElementById('apple_count').innerHTML = apple;
  26.                 break;
  27.         }
  28. }
  29.  
  30. function drag(e){
  31.         e.dataTransfer.setData("data", e.target.id);
  32. }
There you have it we successfully created a Simple Count Object By Dragging 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!

Add new comment