How to Populate Textarea using Drag and Drop in JavaScript

How to Populate Textarea using Drag and Drop in JavaScript


In this tutorial we will create a How to Populate Textarea using Drag and Drop in JavaScript. This tutorial purpose is to teach you how to populate the input textarea with a drag event. This will cover all the basic function that will populate the textarea using drag event. 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 needed a drag and drop feature. I will give my best to provide you the easiest way of creating this program Populate Textarea with drag event. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display the buttons and textare forms. 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="">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 Populate Textarea using Drag and Drop</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-6">
  18. <h3 class="alert alert-success">List</h3>
  19. <h4 id="Avatar" draggable="true" ondragstart="drag(event)">Avatar</h4>
  20. <h4 id="Xmen" draggable="true" ondragstart="drag(event)">Xmen</h4>
  21. <h4 id="Harry Potter" draggable="true" ondragstart="drag(event)">Harry Potter</h4>
  22. <h4 id="Star Wars" draggable="true" ondragstart="drag(event)">Star Wars</h4>
  23. <h4 id="Avenger" draggable="true" ondragstart="drag(event)">Avenger</h4>
  24. <h4 id="Thor" draggable="true" ondragstart="drag(event)">Thor</h4>
  25. <h4 id="Creed" draggable="true" ondragstart="drag(event)">Creed</h4>
  26. <h4 id="Titanic" draggable="true" ondragstart="drag(event)">Titanic</h4>
  27. </div>
  28. <div class="col-md-6">
  29. <div class="form-group">
  30. <textarea class="form-control" id="input" ondrop="drop(event)" ondragover="dragOver(event)" style="resize:none; height:200px;" readonly="readonly"/></textarea>
  31. </div>
  32. <center><button class="btn btn-primary" onclick="submit();">Submit</button></center>
  33. <br />
  34. <div class="alert alert-info">Favorite Movies</div>
  35. <div id="result"></div>
  36. <br /><br />
  37. <button class="btn btn-success" id="btn_reset" style="display:none;" onclick="reset();">Reset</button>
  38. </div>
  39. </div>
  40. <script src="script.js"></script>
  41. </body>
  42. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically populate the textarea when when you drop a string. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var movie_list=[];
  3. function dragOver(e){
  4. e.preventDefault();
  5. }
  7. function drop(e){
  8. e.preventDefault();
  9. var data = e.dataTransfer.getData("data");
  11. document.getElementById('input').value = data;
  12. }
  14. function drag(e){
  15. e.dataTransfer.setData("data",;
  16. }
  18. function submit(){
  19. var input = document.getElementById('input').value;
  20. movie_list.push(input);
  21. var data=movie_list.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. }
  31. function reset(){
  32. document.getElementById('result').innerHTML = "";
  33. movie_list=[];
  34. document.getElementById('btn_reset').style.display="none";
  35. }

In the code above we will use the drag event function namely drop(), dragOver(), drag() method. In dragOver() method we just only prevent the web browser default to interact with our targeted object. While the drag() method we just simple set the dragged object id value to another variable while dragging. Lastly the drop() method will just transfer the dragged id value to the targeted input with the retain value.


The How to Populate Textarea using Drag and Drop 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 Populate Textarea using Drag and Drop 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