Show localstorage data

0

I created a code that stores the data in the localstorage, but now I need the data that is in the localstorage to appear on the screen. So I want to know how I can do this. (Note: I'm 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);
}
    
asked by anonymous 13.03.2018 / 20:24

1 answer

3

To retrieve the value of localStorage you use the syntax:

localStorage.getItem("nome_do_localstorage");

As you have given 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 inside a div :

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

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

Putting everything together in the function, you can do 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>
    
13.03.2018 / 20:31