What is preventing my script from generating new elements within a container?

Hey there, I'm currently working on replicating a 2048 game for a project. Everything seems to be going smoothly except for one thing - the function responsible for creating the board isn't adding any divs to the designated container or anywhere else. Here's the code snippet that should handle this:

document.addEventListener('DOMContentLoader',() => {
const table = document.querySelector(".grid");
const scoreboard = document.getElementById("score");
const resultDisplay = document.getElementById("result");
const width = 4;

function gameBoard(){
    let squares = [];
    for(let i=0; i < width*width; i++){
        square = document.createElement("div");
        square.innerHTML = 0;
        table.appendChild(square);    
        squares.push(square);
    }
}
gameBoard();

The section in my HTML page that needs to be populated is another div with the class "grid" (I made an error in the original, I meant to say class instead of id, my bad for the confusion). Any assistance on this matter would be highly appreciated.

Additionally, here is the relevant part of my HTML structure:

<html>
<head>
<title>.:>2048<:.</title>
<meta charset="utf8">
<link rel="stylesheet" href="estilos/estilo.css">
<script src="scripts/probas.js" charset="utf-8"></script>
</head>
<body>
 <div class="score-container">
     <div class="score-annouce">Puntuación</div>
    <span id="score">0</span>
</div>
<div id="result"></div>
<div class="grid"></div>
</body>
</html>

Answer №1

Change from DOMContentLoader to DOMContentLoaded, as Temani suggested, and remember to end your code with }); - this will resolve the issue and enable correct looping.

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

modify a column in a database table when a different field is chosen

I am working on a basic table with 4 columns, two of which are dropdown menus with the classes "ddm1" and "ddm2". Here is what I am trying to achieve: Users should not be able to select from "ddm2" until they have selected an option from "ddm1". When ...

Keep an eye out for a specific attribute within a Vue component

Is it possible to have an asynchronous operation in the parent component and then notify the child component once it has completed? Can I set up a watch in the child component to detect when a specific property changes, similar to the following example: ...

The client server is unreachable when running the npm run dev command

Having recently ventured into the world of npm and JavaScript, I find myself in a situation where I am attempting to utilize an existing npm project for local testing. Upon entering the command npm run dev, both the server and client scripts are activated ...

I can't figure out why my async delete function for a REST API in a ReactJS app is failing to execute correctly

Why isn't my onclick() remove method working in this react component? I've verified that the DELETE api call functions correctly using Postman and all the information is displayed correctly on the interface. However, when I click the delete butto ...

Is there a way to use CSS to swap out the content of one HTML element with another HTML element's content?

Having an issue with editing or locating a plugin's HTML template on WordPress. Wondering if it is possible to replace the content of one HTML element with another using just CSS? Appreciate any help! ...

JavaScript and Angular are being utilized, incorporating code from various .ts files

Currently, I am working on a project with Angular4. I have successfully created a filter in my app.component.ts which is functioning well in my app.component.html. <button (click)="filterBy('cars')">Cars</button> Even though the fil ...

Retrieve the maximum number of entries from the database and reduce it each time one is selected, using a combination of HTML,

My code is designed to distribute points from a pool into stats through a form. Here is the code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://mynindo.pl/test/css/style.css"> ...

Three.js interpolation surface solution

Greetings everyone, I have a question regarding surfaces in Three.js. I have multiple Vec3 points and I want to create a surface that interpolates through them. While researching, I came across bezier curves (source: three.js bezier - but only as lines) a ...

React class cannot be exported at this time

Here is the content of two files: Product.jsx: class Product extends React.Component{ render(){ return ( <div className='item'> <div className='image'> <img src ...

Personalizing list elements with SVG symbols

I have been experimenting with customizing the bullet points of an unordered list in HTML by using icons that I sourced from various websites. Here is the SVG code for the first icon, which is stored in home.svg: <svg xmlns="http://www.w3.org/2000/sv ...

What is the best way to change the background color of a specific link in the top navigation bar in WordPress?

I'm currently facing a challenge while attempting to create a single link within a WordPress top navigation menu that has a unique background color. Although my approach seems to work on most pages, I've encountered issues where it doesn't ...

Don't display the title if there is no query made in Angular

At the moment, my current setup looks like this: <title ng-bind-template="FuturePhones: {{query}}">FuturePhones</title> However, this results in the title displaying as follows when the page loads: FuturePhones: When I query my controlle ...

A see-through object appears only properly when viewed from one specific angle

I am currently working with THREE.js and the WebGL renderer, facing an issue with a self-transparent object in my scene. This object is composed of a single geometry with a basic material that includes a texture and has the transparent: true property enabl ...

Controlling the Class Attribute in a <td> Element with JavaScript Upon Click Event

I am currently grappling with a situation involving a table with 5 <td> elements. By default, these <td> are red in color. If the user clicks on one of them for the first time, it changes to blue. Subsequent clicks turn it orange and then back ...

Steps for implementing a Material-UI transition effect with hover using pseudo classes

My useStyles hook code is meant to create a full-page background that changes on hover, but for some reason it isn't working. I came across a similar solution in CSS which works perfectly. Can you help me figure out the issue? Code snippet that' ...

Bring in solely the static variable from an ES6 module

I have a file called common.js which holds all the necessary variables and methods used in my App, including a nav-bar module (nav-bar.js) among others. Typically, every module in my app will import the entire common.js module, except for the login module ...

Stop AJAX Submission When Data is Empty

I've been struggling to implement client-side validation to prevent form submission if an object is empty. Despite my efforts, I haven't found a satisfactory solution yet. Here is an excerpt of my form submission Javascript: $(document).ready(f ...

Having trouble choosing an option from the dropdown menu in bootstrap

I'm experiencing an issue with a bootstrap dropdown that I created. I am unable to select an item from it. The code is shown below: var aos = ''; $('.dropdown-item').click(function(event) { event.preventDefault(); // Prevents ...

Developing a webpage linked to a multimedia library

My family has requested that I take on the task of organizing our 'family memories' by creating an access page (which I envision as a web page) that will link to our photos and videos. Each item will be 'tagged' with information such as ...

Delete the content on a webpage using an Ajax jQuery request to transfer it elsewhere

Whenever I submit a form using an ajax post request, I receive values from the controller and manipulate the page based on those values. However, my issue is that I need to erase the values from the previous request when the form is submitted again. For in ...