Function Executing Prior to Parameters Being Accessible

I am currently developing a tic tac toe game using HTMl, CSS (SCSS), and JS. However, I am facing some challenges. The function I created to add an X or O to each grid space seems to be adding them automatically before having the correct parameters, although it somewhat works. Can anyone provide assistance?

You can also check out my progress here.

This is a snippet of my HTML structure:

    <div class="wrap">
      <div class="piece one"></div>
      <div class="piece two"></div>
      <div class="piece three"></div>
      <div class="piece four"></div>
      <div class="piece five"></div>
      <div class="piece six"></div>
      <div class="piece seven"></div>
      <div class="piece eight"></div>
      <div class="piece nine"></div>
    </div>

My CSS styles are as follows:

... ...

Lastly, here is the logic implemented in my JavaScript:

    
   let x = true,
      o = false,
      AI = false,
      easy = true,
      med = false,
      hard = false;
    const one = document.querySelector(".one"),
      two = document.querySelector(".two"),
      three = document.querySelector(".three"),
      four = document.querySelector(".four"),
      five = document.querySelector(".five"),
      six = document.querySelector(".six"),
      seven = document.querySelector(".seven"),
      eight = document.querySelector(".eight"),
      nine = document.querySelector(".nine"),
      X /* The diference between this X and the other x is that this one         is capitalized, same w/ the O and o */ = "<div class='x'><div class='lineOne line'></div><div class='lineTwo line'></div></div>",
      O = "<div class='o'></div>";
...
...

Apologies for the lengthy explanation. I wanted to ensure you have all the necessary details. Thank you for your help!

Answer №1

Make sure to use an anonymous function for all event handlers instead of calling the function directly

one.addEventListener("click", function() {
    updateValue(one)
}, false);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on how to transmit an error message from PHP when handling a jQuery Ajax post request

Greetings! This is my inaugural inquiry, so please understand if I am a bit apprehensive. I am facing an issue in the following scenario... I have an Ajax request structured like this: $.ajax({ url: "test.php", method: "POST", data: { ...

Why is jQuery button function only functioning for the first button?

I'm encountering an issue with my table of records. Each row in the table has a submit button that triggers an AJAX request to the server when clicked. Strangely, the first button works perfectly and sends the request as expected. However, subsequent ...

derive values from radio group inputs

Attempting to compute a selection value based on a radio group and values from additional fields. Any input in the 'Name' field contributes a value to the Total. The value from the radio group needs to be added to this total. Below is the HTML co ...

Angular is utilized to create a dynamic multiple slide carousel that features repeating items

There is a problem with the carousel I am working on where the items are duplicated after resizing the screen. The code I am using is based on Eliseo's solution from this Stack Overflow thread. In his carousel, the arrow functions show/hide depending ...

Iterate over an array containing multiple objects, search for a specific parameter, and append any matching object to a fresh array

Having just started learning JS and React, I've hit a roadblock. Despite scouring through Stack Overflow for answers, I have yet to find a solution with a clear explanation that fits my needs. Let me share the arrays causing my confusion. The first ...

CSS - reduce the size of an element rather than the entire page

Looking to create a page with 2 divs where the first div automatically shrinks and adds scrollbars when the browser window height is decreased, while the second div maintains its height. Wondering if this can be achieved using only CSS. <div class="s ...

Issue of flickering/flashing in Next js when using conditional rendering

I've been attempting to implement localstorage for storing authentication with Next.js. I am using conditional rendering to ensure that localstorage is accessible before displaying the content. However, I am facing an issue where the page flickers or ...

Dealing with a POST array object in JSON format while preventing the occurrence of the ECONNREFUSED error that results

I have a data.json file containing an array of objects with around 4000 entries, each object having approximately 200 properties. However, when I try to read this file and POST it to a loopback API, I encounter an error message stating "ECONNREFUSED Socket ...

Want to learn how to create a draggable uploaded image on a canvas, similar to the functionality of Google

I have a question regarding the draggable feature in Canvas elements. I uploaded an image into the canvas and would like it to be draggable, similar to Google Maps. My ultimate goal is to enable zoom in and out functionalities as well. Is this achievable ...

Having issues with the closest method in DojoQuery on Internet Explorer

Looking for guidance on utilizing the closest method in Dojo to identify the first parent element of an element that needs to be displayed/hidden based on a selected value in a filtering select. It functions correctly in Firefox, but encounters an issue in ...

Repeated information in HTML tables

I am currently working with two HTML tables and two sets of JSON data. Initially, I load one table with the tableData which has a default quantity of 0. In my HTML form, there are three buttons - save, load draft, and edit. Upon clicking on load draft, I p ...

Increase the value of a property within an array of objects based on certain conditions

My task involves incrementing the rank value by one until the name property changes. I am utilizing the page and rank properties to track when this change occurs. In addition, I want to increment it once whenever the type is not equal to none, and then re ...

Exploring the process of traversing a function for each ng-repeat element

Hey everyone, I'm currently facing an issue where I need to pass each date through a function in order to get the desired result. Let's take a closer look at the code snippet: <md-list> <md-divider ></md-divider> ...

Tips for layering one div on top of another div

I'm looking for guidance on how to position my button divs over my Trapezoids in an overlay fashion. Currently, I have set up an array to store the colors and utilizing the map function to match each button with its corresponding color type. When a ...

Creating interactive and customizable web pages with Jquery and HTML

When creating an HTML table with complex logic, it's common to create a separate HTML page for rows like GridRow.html. Then, on the main page, you can use an AJAX request to load the GridRow.html page and receive the result as a string of HTML. In tha ...

Enable users to access their accounts from a single designated computer

As I develop my WebApp using C# .net, I am looking to implement a feature that allows users to connect from only one specific computer. After the user logs in for the first time, I plan to save their PC details and restrict access if they attempt to log in ...

Incorporating an event listener for 'storage' in jQuery using JavaScript

Can anyone tell me how to achieve the same functionality as javascript addEventListener in JQuery? I have been attempting to use it with .bind(), but it seems not to recognize the "storage" keyword. When I tried using 'e' within this context, it ...

How can you position the nav collapse toggle to the far right while still allowing it to collapse downwards on smaller devices with Bootstrap 5?

I am in need of a navigation section that is always visible on all devices, with a collapsible toggle aligned to the right. In Bootstrap 5.2, if I place the collapse toggle after the <div class="collapse navbar-collapse ..., the collapsible conten ...

A guide on extracting the value of a key from an object in angularjs

I stored the data in an object like this: $scope.modelName = { name: {} }; The data was saved as {"APPLE":true,"ORANGE":true}. I am attempting to retrieve only the keys and save them in another object using a for loop. However, I'm having troubl ...

What causes the low Performance score of a default NextJS Application with no content?

Just started experimenting with my initial Next.js project. Upon generating a new project using create-next-app, I decided to test its performance with the web application 'Lighthouse'. Surprisingly, although most other metrics scored above 90, ...