How to Form Word by Dragging the Letters in JavaScript

How to Form Word by Dragging the Letters in JavaScript

Introduction

In this tutorial we will create a How to Form Word by Dragging the Letters in JavaScript. This tutorial purpose is to teach you how to form a words by dragging the letters. This will thoroughly show you the simple way of applying drag and drop function . 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 or application that use drag and drop to build a word. I will give my best to provide you the easiest way of creating this program Form a Word by Dragging. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html list of words and buttons. 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="navabr navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <br />
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">How to Form Word by Dragging the Letters</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="form-group">
  18. <textarea class="form-control" id="input" style="font-size:50px; height:95px; resize:none;" ondrop="drop(event)" ondragover="dragOver(event)" readonly="readonly"/></textarea>
  19. </div>
  20. <br />
  21. <center><button class="btn btn-primary" onclick="submitData();">Submit</button> <button class="btn btn-danger" onclick="removeChar();">Clear</button> <button class="btn btn-success" onclick="clearAll();">Clear All</button></center>
  22. <br /><br />
  23. <div class="col-md-12" style="border:1px solid #000; padding:10px;">
  24. <label id="A" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">A</label>
  25. <label id="B" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">B</label>
  26. <label id="C" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">C</label>
  27. <label id="D" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">D</label>
  28. <label id="E" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">E</label>
  29. <label id="F" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">F</label>
  30. <label id="G" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">G</label>
  31. <label id="H" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">H</label>
  32. <label id="I" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">I</label>
  33. <label id="J" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">J</label>
  34. <label id="K" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">K</label>
  35. <label id="L" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">L</label>
  36. <label id="M" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">M</label>
  37. <label id="N" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">N</label>
  38. <label id="O" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">O</label>
  39. <label id="P" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">P</label>
  40. <label id="Q" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">Q</label>
  41. <label id="R" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">R</label>
  42. <label id="S" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">S</label>
  43. <label id="T" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">T</label>
  44. <label id="U" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">U</label>
  45. <label id="V" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">V</label>
  46. <label id="W" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">W</label>
  47. <label id="X" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">X</label>
  48. <label id="Y" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">Y</label>
  49. <label id="Z" style="font-size:60px; padding:5px;" draggable="true" ondragstart="drag(event)">Z</label>
  50. </div>
  51.  
  52. </div>
  53. <script src="script.js"></script>
  54. </body>
  55. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to form a word by dragging the letters one by one in the form. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  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. function submitData(){
  17. alert(document.getElementById('input').value);
  18. }
  19.  
  20. function removeChar(){
  21. let input = document.getElementById('input');
  22. console.log(input.value);
  23. let val=input.value.slice(0, -1);
  24. input.value=val;
  25. }
  26.  
  27. function clearAll(){
  28. document.getElementById('input').value = "";
  29. }

In the code above we just create only 3 important methods namely drop, drag, dragOver. The drag() function will bind the data of the dragged object. The dragOver() function will just only prevent the web browser from defaulting the drag event. Lastly the drop() function will transfer the dragged data into the target zone and append it into the targeted form

Output:

The How to Form Word by Dragging the Letters 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 Form Word by Dragging the Letters 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