Search code examples
javascripthtmldrag-and-dropundo

How to undo a drag-and-drop in javascript?


On a page for young kids, they have to drag-and-drop one of the 3 signs "<", "=" or ">" between 2 random numbers. This functions once.

After showing them whether they've got it right, I would like to automatically undo the drag-and-drop and display 2 new random numbers. That means sending the sign they chose back to its original position. Something like

WARNING FALSE CODE
move(document.getElementById("up3").innerHTML, document.getElementById("middle3").innerHTML);

My "up3" and "middle3" are divs of the type

<div class="up3" id="up3"></div>

The corresponding classes just deal with positioning, borders and such like.


Solution

  • On a page for young kids, they have to drag-and-drop one of the 3 signs "<", "=" or ">" between 2 random numbers. This functions once.

    After showing them whether they've got it right, I would like to automatically undo the drag-and-drop and display 2 new random numbers. That means sending the sign they chose back to its original position. Something like...

    I can think of 3 ways to do this

    1. Hard reload the page:

      location.reload(true); // reload(false) reloads the browser cache
      window.location.reload();
      
    2. Transfer all code/text inside <div id="a"> into <div id="b">

      document.getElementById("b").innerHTML = document.getElementById("a").innerHTML; // transfer code line
      document.getElementById("a").innerHTML = null;
      

      or

      document.getElementById("b").innerHTML = document.getElementById("a").innerHTML; // transfer code line
      document.getElementById("a").innerHTML = "<div>Empty state code-block</div>";
      
    3. Finally (and more neat approach) create reset() js funciton called whenever you need to

      function reset(){
         document.querySelector("#answer-container").innerHTML = null;
         document.querySelector("#gt-container").innerHTML = ">";
         document.querySelector("#lt-container").innerHTML = "<";
         document.querySelector("#e-container").innerHTML = "=";
         document.querySelector("#gt-container").innerHTML = ">";
         // more code...
      }
      

    I think you should re-state your problem into a bit more specific statement, I'm not really sure if my answer could get the result you desired.