Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!