Skip to content
Home » Forum

Forum

Drag and Drop (Usin...
 
Notifications
Clear all

Drag and Drop (Using Drag and Drop Events)

1 Posts
1 Users
0 Reactions
68 Views
Mark Sikaundi
(@emmanuelmark117)
Member Admin
Joined: 2 years ago
Posts: 101
Topic starter  

Implement drag-and-drop functionality for elements, allowing users to drag items and drop them in a specified area.

Use Case: Drag and Drop File Upload

Allow users to drag files from their computer into a designated area to upload.

Example:

<div id="dropArea" style="width: 200px; height: 100px; border: 2px dashed black;">
  Drop files here
</div>

<script>
  const dropArea = document.getElementById('dropArea');

  dropArea.addEventListener('dragover', function(event) {
    event.preventDefault();
    dropArea.style.backgroundColor = 'lightgray';
  });

  dropArea.addEventListener('dragleave', function() {
    dropArea.style.backgroundColor = '';
  });

  dropArea.addEventListener('drop', function(event) {
    event.preventDefault();
    dropArea.style.backgroundColor = '';
    alert("Files dropped!");
    // Handle file processing here
  });
</script>

   
Quote
Share: