Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
drop/ondrop Event listener not firing on drag and drop

I have a HTML file that has a drop zone for someone to drop a file into and a javascript to process that file. dragenter, dragover, and other events work fine but the drop event does not detect that a file was dropped into the area. Any ideas?

drag.js


let dropArea=document.getElementById("dropZone");
dropArea.addEventListener("drop",function() {
    console.log("File entered");
});


index.html

<!DOCTYPE html>
<html>
  <head>
    <title>sample/title>
    <link rel="stylesheet" type="text/css" href="./style.css" media="screen" />
  </head>
  <body>
    <h1>sample1 </h1>

    <h2>Enter target file name</h2>
    <input id="testForm"></input>
    <button id="myButton">scan</button>
    <div id="dropZone" class="dropZone" >
      <p>Drag your files here</p>
  </div>
    
  </body>
</html>


<script src="index.js"></script>
<script src="drag.js"></script>

I will include the css file as well for continuity sake. style.css

#dropZone{
    background-color: rgb(150, 150, 252);
    color:black;
    text-align: center;
    width:300px;
    height:300px;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are 2 ways to register event listeners in JavaScript:

  1. element.oneventname = function(e) { ... }
  2. element.addEventListener("eventname", function(e) { ... });

You're using option 2, so don't include "on":

dropArea.addEventListener("drop", function() {
  console.log("File entered");
});

Another problem:
Your script tags belong inside the body tag, right before the closing:

    ...
    <script src="index.js"></script>
    <script src="drag.js"></script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda