Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

One way is to keep the innerHtml of that div in LocalStorage and on page refresh check if anything exists there and if exists append in the div.

about 4 years ago · Juan Pablo Isaza Relatório

0

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>

about 4 years ago · Juan Pablo Isaza Relatório

0

You want to create a deep clone before appending the element. This is achieved by calling cloneNode(true) on the element:

document.getElementById("destination").appendChild(element.cloneNode(true));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda