Push in an Array made by Drag and Drop

Asked

Viewed 52 times

0

I have two Ivs, one (div2) that contains 4 images that will be dragged to another div(div1) that has a fixed image. My need is to play the ID values of each image within an array called Answer in drop order, i.e.: If the ID 7 image was placed in the div first, this will be the first number of the array and so on. I’ve been holding on for, like, three hours and I need help. Follows the code>

    <body>      
    <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
            <img src="bloco0.png">
          </div>
          <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
                <image id="5" class = "bloco" src="bloco1.png" draggable="true" ondragstart="drag(event)">
                    <image id="6" class = "bloco" src="bloco2.png" draggable="true" ondragstart="drag(event)">
                        <image id="7" class = "bloco" src="bloco3.png" draggable="true" ondragstart="drag(event)">
                            <image id="8" class = "bloco" src="bloco4.png" draggable="true" ondragstart="drag(event)">
          </div><br>
          <button onclick="check()"> verificar </button>
    <script type="text/javascript" src="js/main.js"></script>
    <script>
        var resposta = [];
            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");
       if(ev.target.tagName == "IMG"){
          ev.target.parentNode.appendChild(document.getElementById(data));
       }else{
          ev.target.appendChild(document.getElementById(data));
       }
    }
    var el1 = document.getElementById('5');//\\
    var el2 = document.getElementById('6');//\\
    var el3 = document.getElementById('7');//\\
    var el4 = document.getElementById('8');//\\

    function check(){
        if(el1.parentElement.id == "div1" && el2.parentElement.id == "div2" && el3.parentElement.id == "div2" && el4.parentElement.id == "div2"){
             resposta.push("5");
             console.log(resposta);
        }
        if(el2.parentElement.id == "div1" && el3.parentElement.id == "div2" && el4.parentElement.id == "div2"){
            resposta.push("4");
            if(el1.parentElement.id=="div1" && resposta[0]!="5"){
                resposta.push("5");
            } 
             console.log(resposta);
        }

    }
  </script>

the function "check()" is an attempt I made and it did not work. I thank you already.

  • Try to add your full source code so we can help you!

1 answer

0


Just put at the end of the function drop() one .push() of id of the dropped image:

function drop(ev) {
   ev.preventDefault();
   var data = ev.dataTransfer.getData("text");
   if(ev.target.tagName == "IMG"){
      ev.target.parentNode.appendChild(document.getElementById(data));
   }else{
      ev.target.appendChild(document.getElementById(data));
   }

   resposta.push(data); // adiciona na array a id da imagem
}

From what I could tell you were trying to do this on the job check(), only in a much more complicated way.

Behold:

var resposta = [];
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");
   if(ev.target.tagName == "IMG"){
      ev.target.parentNode.appendChild(document.getElementById(data));
   }else{
      ev.target.appendChild(document.getElementById(data));
   }
   
   resposta.push(data);
}

//    var el1 = document.getElementById('5');//\\
//    var el2 = document.getElementById('6');//\\
//    var el3 = document.getElementById('7');//\\
//    var el4 = document.getElementById('8');//\\

    function check(){
//        if(el1.parentElement.id == "div1" && el2.parentElement.id == "div2" && el3.parentElement.id == "div2" && el4.parentElement.id == "div2"){
//             resposta.push("5");
//             console.log(resposta);
//        }
//        if(el2.parentElement.id == "div1" && el3.parentElement.id == "div2" && el4.parentElement.id == "div2"){
//            resposta.push("4");
//            if(el1.parentElement.id=="div1" && resposta[0]!="5"){
//                resposta.push("5");
//            } 
//             console.log(resposta);
//        }

             console.log(resposta);
    }
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
   <img width="50" src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg">
</div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
   <image width="50" id="5" class = "bloco" src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" draggable="true" ondragstart="drag(event)">
   <image width="50" id="6" class = "bloco" src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" draggable="true" ondragstart="drag(event)">
   <image width="50" id="7" class = "bloco" src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" draggable="true" ondragstart="drag(event)">
   <image width="50" id="8" class = "bloco" src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" draggable="true" ondragstart="drag(event)">
</div><br>
<button onclick="check()"> verificar </button>

  • again you saving my job. you’re amazing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.