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

182
Visualizações
Call a function on Input Completion

I have a name input on 1.html. I need to call a function where the input will be stored after completion, and when the person clicks ~next~ to go to the next page (2.html), whatever was stored appears there. Example:

~1.html~

What's your name? ~input~ John ~input~

~2.html~

Hi, John! How can i help you?

I know i can use Session Storage to do it, but i'm not sure on how to proceed. Here's what i have:

1.html

<p>"Whats Your Name?"</p>
<input id="your-name-input" type="text">
<a href="2.html">
   <button id="next-button">Next</button>

<script> 

nextButton = document.getElementById("next-button);

nextButton.addEventListener("click", () => {
  var name = document.getElementbyId("your-name-input").value;
  if(name !== "") {
     sessionStorage.setItem("name", name);
     } else {
    alert("Please fill yout name")
});
</script>

And then, on 2.html i have:

<p id="user-name"></p>

What i'm trying to do, is to put inside the <p>, the following greeting: Hi (name.value), how can i help you?

How can i call a function that loads the name value on the 2.html page when the page loads?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The below code should work. There are a few things missing in your code, not sure if you copied everything in.

In either case, the below works for me. You just need to update the link in the window.location = syntax. When you do that, it will take your stored value to the new page in the same tab, and display it using the script code in 2.html.

Code in 1.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1" />
    <title>Your name test</title>

</head>

<body>

    <p>Whats Your Name?</p>
    <input id="your-name-input" type="text">
    <button id="next-button">Next</button>

    <script> 
        const nextButton = document.getElementById("next-button");
        const input = document.getElementById("your-name-input");
        
        nextButton.addEventListener("click", () => {
          const name = input.value;
          if(name !== "") {
             sessionStorage.setItem("name", name);
             window.location = "<link to your 2.html file>";
             } else {
                alert("Please fill your name")
             }
        });

    </script>
    
</body>

<footer>

</footer>
</html>

Code in 2.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1" />
    <title>See, it works</title>

</head>

<body>

    <p id="user-name"></p>

    <script>

        const displayText = document.getElementById("user-name");
        const storedValue = sessionStorage.getItem("name");

        console.log(storedValue);

        window.addEventListener("load", () => {
            displayText.innerHTML = "Hi " + storedValue;
        })

    </script>
    
</body>

<footer>

</footer>
</html>

Original ans to original question:

You could use an addEventListener with an IF statement for your 'next' button and then the code you already have for localStorage.

Depending on what you need from your page, you could also use sessionStorage - that one doesn't save the input forever so might save, albeit limited, space on your user's computer.

I don't see the HTML for your button yet. But assuming you have it, here's an option for the rest of your code in 1.html.

Inside 1.html script tag:

nextButton = document.getElementById("yourButtonID");


nextButton.addEventListener("click", () => {
   var name = document.getElementById("your-name-input").value;
   if(name !== "") { // if input is not empty
      localStorage.setItem("name", name); // set the value in localStorage
      } else {
         alert("Please fill your name")} // else, display an alert (if you like)
   });
about 4 years ago · Santiago Gelvez 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