JavaScript - Drag And Drop Word Builder

Submitted by: 
Language: 
Visitors have accessed this post 135 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Drag And Drop Word Builder</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-success" onclick="reset();">Clear</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;" draggable="true" ondragstart="drag(event)">A</label>
  25. <label id="B" style="font-size:60px;" draggable="true" ondragstart="drag(event)">B</label>
  26. <label id="C" style="font-size:60px;" draggable="true" ondragstart="drag(event)">C</label>
  27. <label id="D" style="font-size:60px;" draggable="true" ondragstart="drag(event)">D</label>
  28. <label id="E" style="font-size:60px;" draggable="true" ondragstart="drag(event)">E</label>
  29. <label id="F" style="font-size:60px;" draggable="true" ondragstart="drag(event)">F</label>
  30. <label id="G" style="font-size:60px;" draggable="true" ondragstart="drag(event)">G</label>
  31. <label id="H" style="font-size:60px;" draggable="true" ondragstart="drag(event)">H</label>
  32. <label id="I" style="font-size:60px;" draggable="true" ondragstart="drag(event)">I</label>
  33. <label id="J" style="font-size:60px;" draggable="true" ondragstart="drag(event)">J</label>
  34. <label id="K" style="font-size:60px;" draggable="true" ondragstart="drag(event)">K</label>
  35. <label id="L" style="font-size:60px;" draggable="true" ondragstart="drag(event)">L</label>
  36. <label id="M" style="font-size:60px;" draggable="true" ondragstart="drag(event)">M</label>
  37. <label id="N" style="font-size:60px;" draggable="true" ondragstart="drag(event)">N</label>
  38. <label id="O" style="font-size:60px;" draggable="true" ondragstart="drag(event)">O</label>
  39. <label id="P" style="font-size:60px;" draggable="true" ondragstart="drag(event)">P</label>
  40. <label id="Q" style="font-size:60px;" draggable="true" ondragstart="drag(event)">Q</label>
  41. <label id="R" style="font-size:60px;" draggable="true" ondragstart="drag(event)">R</label>
  42. <label id="S" style="font-size:60px;" draggable="true" ondragstart="drag(event)">S</label>
  43. <label id="T" style="font-size:60px;" draggable="true" ondragstart="drag(event)">T</label>
  44. <label id="U" style="font-size:60px;" draggable="true" ondragstart="drag(event)">U</label>
  45. <label id="V" style="font-size:60px;" draggable="true" ondragstart="drag(event)">V</label>
  46. <label id="W" style="font-size:60px;" draggable="true" ondragstart="drag(event)">W</label>
  47. <label id="X" style="font-size:60px;" draggable="true" ondragstart="drag(event)">X</label>
  48. <label id="Y" style="font-size:60px;" draggable="true" ondragstart="drag(event)">Y</label>
  49. <label id="Z" style="font-size:60px;" draggable="true" ondragstart="drag(event)">Z</label>
  50. </div>
  51.  
  52. </div>
  53. <script src="js/script.js"></script>
  54. </body>
  55. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.