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

139
Visualizações
change input field value stores in local storage using Javascript

I'm trying to pass an input value from page to the another page.. Using local storage was useful, but when I'm trying to change the value in the second page it doesn't changed, I want to be able to change it..

html code for the first page

<html>
    <head>
        <title>
            Input Form
        </title>
        <link rel="stylesheet" href="style.css" />
        <script>
            function handleSubmit () {
            const name = document.getElementById('name').value;
            const surname = document.getElementById('surname').value;
            localStorage.setItem("NAME", name);
            localStorage.setItem("SURNAME", surname);
            return;
            }
        </script>
    </head>
    <body> 
        <form action="result.html" method="POST">
            <input type="text" id="name" name="name" placeholder="Enter name.." />
            <input type="text" id="surname" name="surname" placeholder="Enter surname.." />
            <input type="submit" onclick="handleSubmit()"/>
        </form>
    </body>
</html>

html code for second page

<html>
    <head>
        <title>
            Result Page
        </title>
        <link rel="stylesheet" href="style.css" />
        <script>
            window.addEventListener('load', () => {
                const name = sessionStorage.getItem('NAME');
                const surname = sessionStorage.getItem('SURNAME');
                document.getElementById('result-name').value = name;
                document.getElementById('result-surname').value = surname;

            })
        </script>
    </head>
    <body>
        <h1>
            Result Page
        </h1>
        <h2>
            Name: <input id="result-name" />
        </h2>
        <h2>
            Surname: <input id="result-surname" />
        </h2>
    </body>
</html>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are using local storage to set the item. You need to use local storage to get the item. Not session storage.

I am talking about these lines here:

const name = sessionStorage.getItem('NAME');
const surname = sessionStorage.getItem('SURNAME');

I changed it for you. Just copy paste the whole page and you are good to go.

    <html>
    <head>
        <title>
            Result Page
        </title>
        <link rel="stylesheet" href="style.css" />
        <script>
            window.addEventListener('load', () => {
                const name = localStorage.getItem('NAME');
                const surname = localStorage.getItem('SURNAME');
                document.getElementById('result-name').value = name;
                document.getElementById('result-surname').value = surname;

            })
        </script>
    </head>
    <body>
        <h1>
            Result Page
        </h1>
        
        <h2>
            Name: <input id="result-name" />
        </h2>
        <h2>
            Surname: <input id="result-surname" />
        </h2>
    </body>
</html>

Read on local storage vs session storage. They are bit different.

about 4 years ago · Santiago Trujillo 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