JavaScript - Populate Textarea When Dragged

In this tutorial we will create a Populate Textarea When Dragged using JavaScript. This code will autofill a HTML textarea when user drag a text element to it. The code use ondrop() method to retrieve and use the element value of the binded id, in order to populate the textarea value base on the dragged element output. 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 - Populate Textarea When Dragged</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <div class="form-group">
  18. <textarea class="form-control" id="input" ondrop="drop(event)" ondragover="dragOver(event)" style="resize:none;" readonly="readonly"/></textarea>
  19. </div>
  20. <center><button class="btn btn-primary" onclick="submit();">Submit</button></center>
  21. <br />
  22. <div class="alert alert-info">MY HOBBIES</div>
  23. <div id="result"></div>
  24. <br /><br />
  25. <button class="btn btn-success" id="btn_reset" style="display:none;" onclick="reset();">Reset</button>
  26. </div>
  27. <div class="col-md-6">
  28. <h4 id="Programming" draggable="true" ondragstart="drag(event)">Programming</h4>
  29. <h4 id="Basketball" draggable="true" ondragstart="drag(event)">Basketball</h4>
  30. <h4 id="Swimming" draggable="true" ondragstart="drag(event)">Swimming</h4>
  31. <h4 id="Horse Riding" draggable="true" ondragstart="drag(event)">Horse Riding</h4>
  32. <h4 id="Cooking" draggable="true" ondragstart="drag(event)">Cooking</h4>
  33. <h4 id="Boxing" draggable="true" ondragstart="drag(event)">Boxing</h4>
  34. <h4 id="Driving" draggable="true" ondragstart="drag(event)">Driving</h4>
  35. <h4 id="Baseball" draggable="true" ondragstart="drag(event)">Baseball</h4>
  36. </div>
  37. </div>
  38. <script src="js/script.js"></script>
  39. </body>
  40. </html>

Creating the Script

This code contains the script of the application. This code will autofill the textarea 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. var hobbies=[];
  2.  
  3. function dragOver(e){
  4. e.preventDefault();
  5. }
  6.  
  7. function drop(e){
  8. e.preventDefault();
  9. var data = e.dataTransfer.getData("data");
  10.  
  11. document.getElementById('input').value = data;
  12. }
  13.  
  14. function drag(e){
  15. e.dataTransfer.setData("data", e.target.id);
  16. }
  17.  
  18. function submit(){
  19. var input = document.getElementById('input').value;
  20. hobbies.push(input);
  21. var data=hobbies.join(" ,");
  22. if(input !=""){
  23. document.getElementById('result').innerHTML = data;
  24. document.getElementById('input').value="";
  25. document.getElementById('btn_reset').style.display="inline";
  26. }else{
  27. alert("Please drag something first!");
  28. }
  29. }
  30.  
  31. function reset(){
  32. document.getElementById('result').innerHTML = "";
  33. document.getElementById('btn_reset').style.display="none";
  34. }
There you have it we successfully created a Populate Textarea When Dragged 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