Show data from localstorage

Asked

Viewed 938 times

1

I created a code that stores the data in the localstorage, but now I need that data that is in the localstorage to appear on the screen. So I want to know how I can do this. (Note: I am developing a form)

(html)

<button id=""cadastro" onclick="cadastro">Cadastrar</button>

(js)

window.onload = function (){
  document.querySelector("#cadastro").addEventListener("click",cadastrar)
}

function cadastrar(){
localStorage.setItem(nomeTabela.value, nomePropriedade.value);
}

1 answer

4


To recover the value of localStorage you use the syntax:

localStorage.getItem("nome_do_localstorage");

How you gave a name from a variable, or you can call by the name you gave or by the variable itself:

localStorage.getItem(nomeTabela.value);

You can play the value within a div:

<div id="minhadiv"></div>

<script>
document.body.querySelector("#minhadiv").innerHTML = localStorage.getItem(nomeTabela.value);
</script>

You put it all together in function, you can do it like this:

<div id="minhadiv"></div>

<script>
function cadastrar(){
    localStorage.setItem(nomeTabela.value, nomePropriedade.value);
    var localsto = localStorage.getItem(nomeTabela.value);
    document.body.querySelector("#minhadiv").innerHTML = localsto;
}
</script>

Browser other questions tagged

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