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

121
Views
how to drag and drop a single image from the displayed array?

i've been trying to code an assignment that displays a bunch of images from an array where you can drag one of them and drop it on the red container, i've managed to show the images but when i drag one image, it seems to drop the whole div, not just that one image. The code below shows the general idea, i promise the real one has way more stuff. I hope someone can help me:(

var images = [ 'https://i.bandori.party/u/i/31Mashiro-Kurata-5yrKez.png', 'https://i.bandori.party/u/i/40CHU²-N46MbU.png'];

function buildImages() {
    // the container
    var container = document.getElementById('content');
    for (var i = 0; i < images.length; i++) {
        // create the image element
        var imageElement = document.createElement('img');
        imageElement.setAttribute('src', images[i]);
        
        // append the element to the container
        container.appendChild(imageElement);
    }
}
// run the script here.
buildImages();

function allowDrop(ev){
        ev.preventDefault();
    }
    function drag(ev){
        ev.dataTransfer.setData("text",ev.target.id);
    }
    function drop(ev){
        ev.preventDefault();
        var data=ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById('content'));
    }
#outfit-container-top{
    display: flex;
    position: absolute;
    justify-content: flex-start;
    width: 203px;
    height: 305px;
    top:100px;
    left: 350px;
    border-radius: 10px;
    background-color: #ff0000;
}
<div id="content" draggable="true" ondragstart="drag(event)"></div>
<div id="outfit-container-top" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

about 4 years ago · Juan Pablo Isaza
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!