How to Transfer Dragged Element to HTML List in JavaScript

How to Transfer Dragged Element to HTML List in JavaScript

Introduction

In this tutorial we will create a How to Transfer Dragged Element to HTML List in JavaScript. This tutorial purpose is to teach you on how transfer the dragged element into html list. This will tackle all the important functionality that will display the html list. 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 if you want apply some drag and drop feature. I will give my best to provide you the easiest way of creating this program Transfer Dragged Element into HTML List. 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 form inputs and HTML list. 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 Transfer Dragged Element to HTML List</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-6">
  18. <div class="form-group">
  19. <input class="form-control" type="text" id="data" ondrop="drop(event)" ondragover="dragOver(event)" readonly="readonly"/>
  20. </div>
  21. <center><button class="btn btn-primary" onclick="transfer();"><span class="glyphicon glyphicon-save"></span> Transfer</button> <button class="btn btn-success" onclick="clearAll();"><span class="glyphicon glyphicon-refresh"></span> Clear All</button></center>
  22. <br />
  23. <h3>=== MY LIST ===</h3>
  24. <div id="result"></div>
  25. </div>
  26. <div class="col-md-6">
  27. <div class="alert alert-info">Movie List</div>
  28. <h4 id="Little Mermaid" draggable="true" ondragstart="drag(event)">Little Mermaid</h4>
  29. <h4 id="Spiderman Into Multiverse" draggable="true" ondragstart="drag(event)">Spiderman Into Multiverse</h4>
  30. <h4 id="Avatar 2" draggable="true" ondragstart="drag(event)">Avatar 2</h4>
  31. <h4 id="Guardian of the Galaxy" draggable="true" ondragstart="drag(event)">Guardian of the Galaxy</h4>
  32. <h4 id="Evil Dead" draggable="true" ondragstart="drag(event)">Evil Dead</h4>
  33. </div>
  34. </div>
  35. <script src="script.js"></script>
  36. </body>
  37. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will transfer the dragged element into list when your submit 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. let data = e.dataTransfer.getData("data");
  8.  
  9. document.getElementById('data').value = data;
  10. }
  11.  
  12. function drag(e){
  13. e.dataTransfer.setData("data", e.target.id);
  14. }
  15.  
  16. function transfer(){
  17. let data = document.getElementById('data').value;
  18.  
  19. if(data !=""){
  20. document.getElementById('result').innerHTML += data+"<br />";
  21. }
  22. }
  23.  
  24. function clearAll(){
  25. document.getElementById('result').innerHTML = "";
  26. document.getElementById('data').value = "";
  27. }

This code above uses only one method to transfer the element into html list called transfer(). This function will transfer the dragged element into html list after you submit the form inputs. To transfer a dragged element we just simply get the dragged data via the drag event then put it into the html form in order to submit the inputs.

Output:


The How to Transfer Dragged Element to HTML List 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 Transfer Dragged Element to HTML List 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