0

I need to link when i click "Submit" button to open another html page and to change text on it.

my html code

<div class="welcome">
      <h2 id="welcomeHeader">Welcome to Quiz</h2>
      <h3 id="nameHeader">Write Your Name</h3>
      <input id="userName">
      <button type="submit" onclick="start()">Let's Go!</button>
    </div>

my quiz code

<div>
        <h2 id="welcomeText">Hello</h2>
    </div>

my JS code

    let userName = document.getElementById('userName');
    let nameHeader = document.getElementById('nameHeader');
    let welcomeText = document.getElementById('welcomeText');

    function start() {
        window.location.href='quiz.html';
        welcomeText.innerHTML = 'Hello ' + userName.value; + ', welcome to Quiz';  
}

Code opening quiz.html page but it doesn't rewrite text in #welcomeText?

Any help?

I tried different solutions but nothing works. I need all kind of help.

gajkez
  • 1
  • 4
    Pass the name in the URL as a querystring parameter, use local storage, or a cookie. – j08691 Apr 19 '23 at 14:59
  • 2
    Does this answer your question? [Share data between HTML pages](https://stackoverflow.com/questions/11609376/share-data-between-html-pages) – 0stone0 Apr 19 '23 at 15:12
  • Wait what? How do you expect that to work?? Once you redirect the user to another page, the next line of code would not work... Or am I missing something here? – Archit Gargi Apr 19 '23 at 16:08

1 Answers1

0

You would have to put welcomeText.innerHTML = 'Hello ' + userName.value; + ', welcome to Quiz'; as part of the code on the actual code for the game page.

Shlomo
  • 3
  • 3
  • Thanks a lot for your answer. I'm new in this so honestly it's not totaly clear for me HOW to do that but I'll try to figure it out. Thanks again! – gajkez Apr 19 '23 at 15:59
  • Does that mean that I need separate script.js as file (for ex. script2.js) for for quiz.html page? – gajkez Apr 19 '23 at 17:01