Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Problems With Javascript Tick Tack Toe Game

I'm trying to create a tick tack toe game using javascript. I've posted my code up in github, here: https://github.com/chaneth8/Tick-Tack-Toe. The main problem I'm having is with updating the users' names. I created a form for users to add their names in, as seen on the top of the page: [![enter image description here][1]][1]

When users enter their names into the form however, the headings that say "Player one is" and "Player two is:" remain unchanged. A similar thing happens when a user wins a game. Again, their name isn't displayed - it just says "won" at the bottom of the page, instead of " won", even after both users enter their name into the form at the top of the page. The way my code works is as follows: I wrote the following HTML for my form:

    <div id = "input-form">
        <form onsubmit='return Newgame.startGame()' name = 'form' id = 'form'>
            Input Player 1's Name:<input type= 'text' name='player1'>
            Input Player 2's Name:<input type= 'text' name='player2'>
            <input type='submit' value='Submit'>
        </form>

    </div>

I then defined a factory function, in Javascript, that had the function startGame in it:

   const game = () => {

        let Board = [[2,2,2], [2,2,2], [2,2,2]];
        let Current_player = 0;
        let player0 = "";
        let player1 = "";

        const startGame = () => {
            player0 = document.forms["form"]["player1"].value; 
            player1 = document.forms["form"]["player2"].value;

            clear();
            Initialize();
        }

I then called an instance of the factory function at the bottom of the page:

    let Newgame = game();
    Newgame.Initialize();

I'm wondering why these issues are happening, and how I can fix it. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to prevent the form from being submitted, after onSubmit the page refreshes and the value of player0 and player 1 is lost

pass the event in the onSubmit

onsubmit="return Newgame.startGame(event)"

end prevent in startGame

 const startGame = event => {
        event.preventDefault();
        ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!