JavaScript - Drag And Drop Word Builder

In this tutorial we will create a Drag And Drop Word Builder using JavaScript. This code will display a word when user drag a letter to the drop target. The code use ondrop() method to get and use binded id value, in order to populate the drop zone based on the dragged element value. 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 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 - Drag And Drop Word Builder</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="form-group">
  17. <textarea class="form-control" id="input" style="font-size:50px; height:95px; resize:none;" ondrop="drop(event)" ondragover="dragOver(event)" readonly="readonly"/></textarea>
  18. </div>
  19. <br />
  20. <center><button class="btn btn-success" onclick="reset();">Clear</button></center>
  21. <br /><br />
  22. <div class="col-md-12" style="border:1px solid #000; padding:10px;">
  23. <label id="A" style="font-size:60px;" draggable="true" ondragstart="drag(event)">A</label>
  24. <label id="B" style="font-size:60px;" draggable="true" ondragstart="drag(event)">B</label>
  25. <label id="C" style="font-size:60px;" draggable="true" ondragstart="drag(event)">C</label>
  26. <label id="D" style="font-size:60px;" draggable="true" ondragstart="drag(event)">D</label>
  27. <label id="E" style="font-size:60px;" draggable="true" ondragstart="drag(event)">E</label>
  28. <label id="F" style="font-size:60px;" draggable="true" ondragstart="drag(event)">F</label>
  29. <label id="G" style="font-size:60px;" draggable="true" ondragstart="drag(event)">G</label>
  30. <label id="H" style="font-size:60px;" draggable="true" ondragstart="drag(event)">H</label>
  31. <label id="I" style="font-size:60px;" draggable="true" ondragstart="drag(event)">I</label>
  32. <label id="J" style="font-size:60px;" draggable="true" ondragstart="drag(event)">J</label>
  33. <label id="K" style="font-size:60px;" draggable="true" ondragstart="drag(event)">K</label>
  34. <label id="L" style="font-size:60px;" draggable="true" ondragstart="drag(event)">L</label>
  35. <label id="M" style="font-size:60px;" draggable="true" ondragstart="drag(event)">M</label>
  36. <label id="N" style="font-size:60px;" draggable="true" ondragstart="drag(event)">N</label>
  37. <label id="O" style="font-size:60px;" draggable="true" ondragstart="drag(event)">O</label>
  38. <label id="P" style="font-size:60px;" draggable="true" ondragstart="drag(event)">P</label>
  39. <label id="Q" style="font-size:60px;" draggable="true" ondragstart="drag(event)">Q</label>
  40. <label id="R" style="font-size:60px;" draggable="true" ondragstart="drag(event)">R</label>
  41. <label id="S" style="font-size:60px;" draggable="true" ondragstart="drag(event)">S</label>
  42. <label id="T" style="font-size:60px;" draggable="true" ondragstart="drag(event)">T</label>
  43. <label id="U" style="font-size:60px;" draggable="true" ondragstart="drag(event)">U</label>
  44. <label id="V" style="font-size:60px;" draggable="true" ondragstart="drag(event)">V</label>
  45. <label id="W" style="font-size:60px;" draggable="true" ondragstart="drag(event)">W</label>
  46. <label id="X" style="font-size:60px;" draggable="true" ondragstart="drag(event)">X</label>
  47. <label id="Y" style="font-size:60px;" draggable="true" ondragstart="drag(event)">Y</label>
  48. <label id="Z" style="font-size:60px;" draggable="true" ondragstart="drag(event)">Z</label>
  49. </div>
  50.  
  51. </div>
  52. <script src="js/script.js"></script>
  53. </body>
  54. </html>

Creating the Script

This code contains the script of the application. This code will display a world 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. 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('input').value += data;
  10. }
  11.  
  12. function drag(e){
  13. e.dataTransfer.setData("data", e.target.id);
  14. }
  15.  
  16.  
  17. function reset(){
  18. document.getElementById('input').value = "";
  19. }
There you have it we successfully created a Drag And Drop Word Builder 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