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

133
Vistas
How can I add a dragged item to an array once it has been dropped to the drop target in JavaScript and then display it in HTML?

I want to get the data from each dragged item once it has been dropped and add it to an array. I am relatively new to Javascript so I'm unsure how I can achieve this. I want 'data' added to the 'total' array each time an item is dropped, this is what I have so far:

function dropHandler(ev) {
  ev.preventDefault();
  
  const totalBalance = [];
  var data = ev.dataTransfer.getData("text");
  var dropTarget = document.getElementById('drop-target');
  
  ev.target.appendChild(document.getElementById(data));
  
  // changes colour of drop target when dragged over
  ev.currentTarget.style.background = "black";
  
  totalText = document.getElementById('totalText');
  totalText.innerHTML = totalBalance
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

With the list you have created, you can simply just push new values into it with push(value), this inserts the given value into the list.

let totalBalance = []; // Table to track all of the balances.

function dropHandler(ev) {
  ev.preventDefault();
  
  var data = ev.dataTransfer.getData("text");
  var dropTarget = document.getElementById('drop-target');
  
  ev.target.appendChild(document.getElementById(data));
  
  // changes colour of drop target when dragged over
  ev.currentTarget.style.background = "black";

  // Insert this data into our total balance's list.
  totalBalance.push(data);
  
  totalText = document.getElementById('totalText');
  totalText.innerHTML = totalBalance
}

The totalBalance list is defined before the function definition so it is not overwritten every time something is dropped.

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