Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda