Help using setItem and getItem from localStorage

Asked

Viewed 41 times

0

I’m trying to save an answer, but I’m not getting it every time I refresh the previous answer back. Follow what I’m trying to implement:

<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="utf-8">
        <title>question and answer</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <style>

        </style>
    </head>
    <body>
        <p>
        What is the capital of Brazil?   
        </p>
        <p id="resposta">
        Rio de Janeiro   
        </p>

    <script>
        $(function () {
            $("p").dblclick(function () {
                var conteudoOriginal = $(this).text();

                $(this).addClass("respostaEmEdicao");
                $(this).html("<input type='text' value='" + conteudoOriginal + "' />");
                $(this).children().first().focus();

                $(this).children().first().keypress(function (e) {
                    if (e.which == 13) {
                        var novoConteudo = $(this).val();
                        $(this).parent().text(novoConteudo);
                        localStorage.content = $('#resposta').html();
                        $('#resposta').html(localStorage.content);
                        $(this).parent().removeClass("respostaEmEdicao");
                    }  
                });


            $(this).children().first().blur(function(){
                $(this).parent().text(conteudoOriginal);
                $(this).parent().removeClass("celulaEmEdicao");
            });
            });

        });
    </script>

    </body>

</html> 

What am I doing wrong?

1 answer

2

Try it this way:

First you put your data in localStorage and name it:

localStorage.setItem('Content', $('#resposta').html());

Then you take it from localStorage by the name you put:

$('#resposta').html(localStorage.getItem('Content'));

This will return the value you assigned.

I hope I’ve helped

Browser other questions tagged

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