Error trying to use HTML5 Local Storage

Asked

Viewed 288 times

0

I’m studying about local Storage in HTML5 and JS, understanding that its purpose is to store texts in the local Storage of browser.

When testing, I got JS errors. I’d like to understand better why:

Printscreen do erro

<script type="text/javascript">

function exibir(){
    document.getElementById("nome").value = localStorage.getItem("l_nome");
}

</script>
<body onload="exibir();">
    <form>
        Nome: <input type="text" id="nome">
        <input type="button" onclick="salvar();" value="Save">
    </form>
</body>
  • 2

    Where is your method salvar? The error is that it was not set. There is no error with localStorage.

1 answer

5

Only set the SAVE function:

<script type="text/javascript">

function exibir(){
    document.getElementById("nome").value = localStorage.getItem("l_nome");
}

function salvar(){
    localStorage.setItem("l_nome", document.getElementById("nome").value);
}

</script>
<body onload="exibir();">
    <form>
        Nome: <input type="text" id="nome">
        <input type="button" onclick="salvar();" value="Save">
    </form>
</body>
  • I made a demonstrative fiddle: http://jsfiddle.net/s7e53t3r

  • Thank God it was "just" that :) ... +1

  • many thanks to all.

Browser other questions tagged

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