JavaScript - Autofill Inputs By Dragging

In this tutorial we will create a Autofill Inputs 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:

First you have to download bootstrap framework, 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="navabr 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 - Autofill Inputs By Dragging</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <h4 id="PHP" draggable="true" ondragstart="drag(event)">PHP</h4>
  18. <h4 id="JavaScript" draggable="true" ondragstart="drag(event)">JavaScript</h4>
  19. <h4 id="C++" draggable="true" ondragstart="drag(event)">C++</h4>
  20. <h4 id="C#" draggable="true" ondragstart="drag(event)">C#</h4>
  21. <h4 id="Python" draggable="true" ondragstart="drag(event)">Python</h4>
  22. </div>
  23. <div class="col-md-6">
  24. <div class="form-group">
  25. <input class="form-control" type="text" id="data" ondrop="drop(event)" ondragover="dragOver(event)" readonly="readonly"/>
  26. </div>
  27. <center><button class="btn btn-primary" onclick="submit();"><span class="glyphicon glyphicon-save"></span> Submit</button> <button class="btn btn-success" onclick="reset();"><span class="glyphicon glyphicon-refresh"></span> Clear</button></center>
  28. <br />
  29. <div id="result"></div>
  30. </div>
  31. </div>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </html>

Creating the Script

This code contains the script of the application. This code can drag and drop the data into the corresponded input form. 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. function dragOver(e){
  2. e.preventDefault();
  3. }
  4.  
  5. function drop(e){
  6. e.preventDefault();
  7. var data = e.dataTransfer.getData("data");
  8.  
  9. document.getElementById('data').value = data;
  10. }
  11.  
  12. function drag(e){
  13. e.dataTransfer.setData("data", e.target.id);
  14. }
  15.  
  16. function submit(){
  17. var data = document.getElementById('data').value;
  18.  
  19. if(data !=""){
  20. document.getElementById('result').innerHTML = data;
  21. }
  22. }
  23.  
  24. function reset(){
  25. document.getElementById('result').innerHTML = "";
  26. document.getElementById('data').value = "";
  27. }
There you have it we successfully created a Autofill Inputs 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