Push in an Array made by Drag and Drop

0

I have two divs, one (div2) that contains 4 images that will be dragged to another div (div1) that has a still image. My need is to play the ID values of each image within an array called Response in drop order, ie: If the ID 7 image was placed in the first div, this will be the first number in the array and so on. I've been hooked for 3 hours and I need help. Follow 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 did not work. Thanks in advance.

    
asked by anonymous 26.11.2018 / 04:11

1 answer

0

Just put at the end of the function drop() a .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
}

As far as I could tell you were trying to do this in the check() function, but in a much more complicated way.

See:

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><divid="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>
    
26.11.2018 / 13:17