Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Moving and keeping an element

Once I press "+" sign and the element is moved to the "destination" div, what would be the easiest way with the help of local storage to keep an "element" in the "destination" div after the page is refreshed?

function appendIt() {
  var element = document.getElementById("element");
  document.getElementById("destination").appendChild(element);
}
#destination {
  background-color:red;
}

#source {
  background-color:beige;
}
<div id="destination"></div>
<div id="source">
  <a id="element"href="#" onclick="appendIt()">One</a></div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As you mentioned with localStorage, you can add some flag in local storage inside appendIt() function. And on windows load event you can check if that flat is there in local storage and what value it have. Based on that value you can again call appendIt() as per requirement.

function appendIt() {
  // set value to local storage with key append
  localStorage.setItem("append", "true");
  var element = document.getElementById("element");
  document.getElementById("destination").appendChild(element);
}

// Add windows load event to check local storage
window.onload = (event) => {
  // check local storage for key append and if its value is true then call appendIt function
  if (localStorage.getItem("append") == "true") {
    appendIt();
  }
};
#destination {
  background-color: red;
}

#source {
  background-color: beige;
}
<div id="destination"></div>
<div id="source">
  <a id="element" href="#" onclick="appendIt()">One</a></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading