Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu December 21, 2022 › #HowTo #Javascript

How to display text on another file after clicking a button

In this article, you will learn how to display text from one page to another after clicking a button using JavaScript.

For this, we will use localStorage. In the first page, we will save input data in localStorage and then we will display those data in another page.

First page.

<html>
<head>
    <title>First HTML page</title>
</head>
<body>
    <form id="form">
       <input type="text" id="input" value="Some Text">
      <button type="submit">Click Me</button>
    </form>
    <script>
        document.getElementById("form").addEventListener("submit", (e) => {
            
            let myVal = document.getElementById('input');
            localStorage.setItem('input', myVal.value);
            window.location.href = "another-page.html";

           e.preventDefault();
        });
    </script>
</body>
</html>

Second page.

<html>
<head>
  <title> Second page</title>
</head>
<body>
  <textarea id="input"></textarea>
 <script>
    window.onload = function(){
      document.getElementById('input').value = localStorage.getItem('input');
    };
</script>
</body>
</html>
save
listen
AI Answer
Write Your Answer
loading
back
View All