When an external image is dragged over, include the class in the drop area of the file input

Hey, does anyone know how to use Jquery to add a class when an element is being dragged over a drop area?

Here's the HTML code I'm working with:

<div class="upload">
   <form action="">
      <input class="uploadfile" type="file" id="imageLoader" name="imageLoader">
      <div class="circle"><i class="flaticon-arrows-1"></i></div>
      <div class="text">Drag and Drop or browse from your computer</div>
   </form>
</div> 

I would like to add a class to the upload div so it looks like this:

<div class="upload dragging"></div>

Answer №1

To achieve this functionality using JavaScript, you need to "listen" for the dragover event.

var target = document.querySelector('.target');

target.addEventListener("dragover", function( event ) {
  this.classList.add('dragging');
}, false);

target.addEventListener("drop", function( event ) {
  this.classList.remove('dragging');
}, false);
.dragging {
  border:1px solid green;  
}
<div class="target">
  <form action="">
    <input class="uploadfile" type="file" id="imageLoader" name="imageLoader">
    <div class="circle"><i class="flaticon-arrows-1"></i></div>
    <div class="text">Drag and Drop or browse from your computer</div>
  </form>
</div>

Answer №2

If you want to eliminate the dragging class once you exit the drag area, simply include this snippet in Mosh Feu's code:

upload.addEventListener("dragleave", function( event ) {
    this.classList.remove('dragging');
}, false);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jquery submit is not functioning as expected

I need assistance with a form <form id="formModLezione" method="post"> Currently, I am attempting to execute the following code: var messaggio=""; var url = "EsistonoIscritti"; var xmlHttp = new XMLHttpRequest(); xmlHttp.open("POST", url, ...

Move divs that are currently not visible on the screen to a new position using CSS animations

Upon entering the site, I would like certain divs to animate from an offscreen position. I came across this code snippet: $( document ).ready(function() { $('.box-wrapper').each(function(index, element) { setTimeout(function(){ ...

Building a WCF Rest Service for Sending Large Files to ASP.NET MVC: A Step-by-Step Guide

Is it possible to utilize WCF in developing an endpoint for uploading large files on a website through an ASP.NET MVC controller method, while updating a jQuery progress bar in the browser using Ajax or Json? What are the steps involved in accomplishing t ...

AngularJS checkbox validation requires a minimum of two checkboxes to be selected

When using AngularJS, I am looking to create a validation rule where a minimum of 2 checkboxes must be checked for the input to be considered valid. Here is what I have attempted: <div ng-repeat="item in items"> <label><input type="chec ...

Learn the process of obtaining a location and showcasing it on Google Maps

I am working on creating a program that utilizes modernizer to ask for the user's location and then displays it using Google Maps. So far, I have been able to use geolocation to determine the coordinates of the user's location and attempted to in ...

Positioning a DIV in the center within Bootstrap columns

I have been attempting to solve the challenge of vertically aligning content within my bootstrap grid. Within my 3 col-md-4 columns, I am looking to center another div along with text inside each of them. This is the current appearance: https://i.sstati ...

Displaying a div when hovering over it, and keeping it visible until clicked

I want to display a div when hovering over a button. The shown div should be clickable and persistent even if I move the cursor from the button into the shown div. However, it should be hidden when moving out of the entire area. I'm unsure about how ...

How to retrieve client's hostname using Node and Express

How can the client's hostname be retrieved in a Node / Express server? Is there a method similar to req.connection.remoteAddress that can be used to obtain the client's hostname? ...

Disable the mousedown event in JavaScript/jQuery

During a drag and drop operation, I have set certain limits on the screen. When the draggable object passes these limits, I want it to return to its original position. The issue I am facing is that if the onmousedown event is active, the object does not r ...

Tips for programmatically choosing dropdown menus in Angular 5

Trying to set up a dropdown select option in Angular 5 that dynamically changes based on the backend's input. https://i.sstatic.net/8cgsh.png Keep in mind that both select boxes are identical, only the options inside them vary. selector.html <h ...

sending multiple checkbox selections to a PHP script using jQuery

<form id="foo"> <input type="text" name="voucher" placeholder="voucher ID" class="bill_fillter"/> <input type="text" name="voucher" placeholder="voucher ID" class="bill_fillter"/> <input type="text" name="voucher" placeholder="voucher ...

What is the best way to expand the width of a table cell in a React + Material project?

Currently, I am utilizing material UI in conjunction with React to display my data in a table format. Within the table, there are specific titles such as "GENERAL_INFORMATION" and "INFORMATION" that I intend to center align. However, I have encountered an ...

Issue with Next.js: Setting the width to 100vh prevents the height from being responsive on mobile devices

While I understand that using height: 100vh on mobile is generally discouraged for various reasons, I'm curious as to why height: 100vh paired with width: 100vh in Next.js doesn't produce the expected result. Instead of a full-height square, I en ...

What is the technique for arranging the display of a component in React?

Is there a way to dynamically render a component in my react-app at a specific date and time, like 6.00PM on October 27, 2022? I want to release a form for signing up starting from that exact moment. The timestamp information will be stored in my database ...

Issues with displaying Base64 images in Fancybox2 on Internet Explorer

I want to incorporate a base64 image into fancybox. This is the code I am using: <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script> <link rel="stylesheet" href="http://cdnjs.cloudflare ...

What is the best way to incorporate variables into the useState hook in React?

I want to create multiple useState hooks based on the length of the userInputs array. The naming convention should be userInput0, userInput1, userInput2,...userInputN. However, using "const [userInput+index.toString(), setUserInput] = useState('' ...

Adjust the template within a directive to dynamically include an additional directive

Challenge Create a custom directive that dynamically adds the ng-bind attribute, allowing for the use of ng-bind, ng-bind-html, or ng-bind-html-unsafe without needing to manually add it to the template definition throughout. Illustrative Example http://j ...

Generating radio buttons dynamically and automatically selecting the correct button using AngularJS

In the questionnaire form, each question is looped over along with its answers to create radio buttons for each answer. The answer object contains a property called answered, which can be true or false depending on whether the answer has been previously ...

Steps for executing a Node script are as follows:

My task is to execute a node script that will remove an object from an external API. This can be achieved by running the following command: node server.js Customer55555 Upon execution, the specified object should be deleted successfully. To interact wit ...

Struggling to eliminate the underlines from an HTML hyperlink

Can someone help me figure out how to remove the annoying underline link that pops up when hovering over an a element in HTML? I attempted adding a style="text-decoration: none" attribute to the a link, but the underline persists when I hover over the text ...