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

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

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 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