How to Dynamically Remove HTML List When Dragging in JavaScript

How to Dynamically Remove HTML List When Dragging in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Remove HTML List When Dragging in JavaScript. This tutorial purpose is to teach you how to remove a list using drag and drop event. 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 feature to modify a data. I will give my best to provide you the easiest way of creating this program Remove a List using Drag and Drop. 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 and an image. 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="navbar 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">How to Dynamically Remove HTML List When Dragging</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <h1>List of Anime</h1>
  18. <ul>
  19. <h3><li id="list1" draggable="true" ondragstart="drag(event)">One Piece</li></h3>
  20. <h3><li id="list2" draggable="true" ondragstart="drag(event)">Naruto</li></h3>
  21. <h3><li id="list3" draggable="true" ondragstart="drag(event)">Pokemon</li></h3>
  22. <h3><li id="list4" draggable="true" ondragstart="drag(event)">Dragon Ball</li></h3>
  23. <h3><li id="list5" draggable="true" ondragstart="drag(event)">Bleach</li></h3>
  24. </ul>
  25.  
  26. </div>
  27. <div class="col-md-4">
  28. <img src="image/trash.png" height="200" width="200" ondrop="drop(event)" ondragover="dragOver(event)"/>
  29. </div>
  30. </div>
  31. <script src="script.js"></script>
  32. </body>
  33. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove a list when you drop it in the trash. 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. let el = document.getElementById(data);
  9. let name = el.innerHTML;
  10.  
  11. el.parentNode.removeChild(el);
  12. alert("You remove "+name+" on the list!");
  13.  
  14. }
  15.  
  16. function drag(e){
  17. e.dataTransfer.setData("data", e.target.id);
  18. }

In the given code we just create 3 major method called drop, drag, dragOver. The drag() function will bind the data of the drag 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 drop zone, and will process a certain event the will remove the dragged object on the list.

Output:

The How to Dynamically Remove HTML List When Dragging 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 Dynamically Remove HTML List When Dragging 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