Revising Data in JavaScript Tables

I'm facing an issue with a 2D array, cells, which stores cellData values. There's a JavaScript function called displayTable that I use to present this array as a table. The problem arises when clicking on a cell – its cellType is supposed to change to "start", and upon redrawing the table immediately, its color should also change accordingly. However, the cell's color remains unchanged.


function CellData(cellType) {
    this.cellType = cellType;
    this.crowFlyDistance = Number.Infinity;
    this.backtrack = null;
}

function displayTable() {
    var table = document.createElement('table');
    var tableBody = document.createElement('tbody');
    for (var row = 0; row < rowAndColumnSize; row++) {
        var rowToAppend = document.createElement('tr');
        for (var column = 0; column < rowAndColumnSize; column++) {
            var cellToAppend = document.createElement('td');
            cellToAppend.className = "baseCell";
            cellToAppend.className += " " + cells[row][column].cellType;
            cellToAppend.addEventListener("click", clicked);
            rowToAppend.appendChild(cellToAppend);
        }
        tableBody.appendChild(rowToAppend);
    }
    table.appendChild(tableBody);
    document.getElementById("space").appendChild(table);
}

Initially, the clicked function is linked to selectStart. Once the user chooses a start location on the table, the clicked function should clear the table using clearTable and then call displayTable to update the table's values.


function selectStart() {
    var row = this.parentNode.rowIndex;
    var column = this.cellIndex;
    if (cells[row][column].cellType == "empty") {
        cells[row][column].cellType = "start";
        clearTable();
        displayTable();
        clicked = selectEnd();
        alert("Select an end location.");
    } else
        alert("Select a cell that is empty.");
}

function clearTable() {
    var tableDiv = document.getElementById("space");
    while (div.firstChild) {
        div.removeChild(div.firstChild);
    }
}

Each cell has different styles defined by its class, so when a cell is clicked, its color should change. Nonetheless, I'm experiencing an issue where the clicked cell retains its original color. Even setting the text of each cell to show its cellType results in it showing up as empty. Strangely, if I click on the same cell consecutively, the "Select an empty cell" alert pops up, suggesting that the cells.cellType value has been updated. Am I missing something in how I'm updating my table?

JSFiddle Link

Answer №1

Your JSFiddle contains some issues, specifically when the clearTable method is invoked, it causes a crash. To effectively remove all elements within the space element, you can utilize space.innerHTML = ''

function clearTable() {
    space.innerHTML = '';
}

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

UI Select not found result event

Currently implementing https://github.com/angular-ui/ui-select and looking for a solution to trigger an event (such as opening a modal) when no results are found while using a filter. <ui-select ng-model="SelectedCustomer.selected" theme="select2"> ...

Can the dashstyle be configured for a solid-gauge chart in Highcharts?

I am having trouble making the border dashed on my pie chart. It seems that the solution provided for solidgauge charts is not working as expected. Interestingly, the graph property is undefined for solidgauge charts. I wonder if the dashed style could be ...

Simple method for transforming &#XXXX; from HTML to UTF-8 XML, whether through code in .Net or with the help of various software solutions

These examples are provided to show that HTML and XML are not the same. When an HTML file is inputted, it looks like this: <p class=MsoNormal style='tab-stops:.5in'><b><span style='mso-tab-count:3'> </span>< ...

Consistent Failure: jQuery File Upload consistently aborts with the error message 'File upload aborted'

I'm currently utilizing the Blueimp File Upload Plugin to facilitate file uploads to a remote server. The HTML code snippet for the file upload input is as follows: <input id="fileupload" type="file" name="files[]" data-url="http://my-server-ip/u ...

Which programming language or technology utilizes the syntax <%VARIABLE%> and in what context is it employed?

First things first, I want to clarify that I'm not a developer, so this might be a simple question. Despite my research indicating that ASP typically uses this syntax, it's important to note that it is not the case here. I am currently delving i ...

JavaScript if statement to check for either one, but not both

Hey there, fellow developers. I'm currently encountering a mental block and struggling to find a solution for my issue. Here is the code snippet in question: if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0)) { ...

Can Selenium be utilized with conditional if statements?

<game-tile letter="g" evaluation="correct" reveal=""></game-tile> Currently, I am developing a program in Python using Selenium to automatically guess the wordle word. I am exploring the possibility of implementin ...

Ensuring proper integration of this regular expression for password validation

Trying to create a jQuery function that can efficiently check if passwords meet specific requirements. The password must contain at least 3 characters that are uppercase, numbers, or special characters, in any combination. I currently have a function usi ...

Exploring the process of mapping an array with unidentified object properties

Here is a new way to write it: const array = [ {name: 'John', age: 30} {name: 'Alice', age: 25} {name: 'Bob', age: 40} ] function DisplayComponent({array}) { return ( {array.map(( {name, age} ) => ( <p ...

Deliver the react package loaded with redux dependencies via ship

In the process of developing an NPM package containing React components from a different company. The components include both pure UI components following the UI/Container pattern, as well as components that are integrated with redux functionality. The ai ...

Are there any notifications triggered when a draggable element is taken out of a droppable zone?

Within a single droppable area, there is a collection of individual Field Names that can be dragged, and a table featuring X headers, each of which can be dropped into. Initially, these headers are empty. Is there a way to detect when an item is taken out ...

Encountered a problem while trying to import a component from a forked

Currently, I'm in the process of developing a custom chat tool using react-chat-elements as my UI library for chat components. However, I've encountered an issue with modifying CSS values to better suit my needs while avoiding reliance on the out ...

Ways to conceal the value of a button element?

Is there a way to conceal the text on a button without hiding the entire button? I attempted using color: transparent; but to no avail. The goal is to hide only the text within the button, while retaining the value attribute. The value should remain in ...

Having trouble with my ajax success function, could use some assistance please

Having issues passing values from an AJAX request to a PHP file. The error function seems to be working, but I can't figure out what's wrong with my code. Here is what I have: <script> function test(){ //alert("hello"); var myD ...

Exploring the functionality of multiple index pages in angularjs

I am trying to access the localhost:3000/admin which is located in my views. The index.html and admin.html are two separate base files, one for users and the other for the admin dashboard respectively. Here is a snippet from my app.routes.js file: angul ...

What are the reasons for avoiding placing CSS code directly within HTML?

Situation: My website is dynamically served, with all CSS and javascript directly embedded into the HTML document to optimize speed. Advantages: Reduces web requests Fewer files downloaded Speeds up webpage loading time Disadvantages: Potential cachin ...

Creating a list repeater using v-for in Vue.js 2 with computed property

Seeking assistance with adding computed columns to a table (last three columns). Suspecting the issue lies in the computed property not correctly referencing the record. Any simple solutions that I might be overlooking? Appreciate any thoughts or insights! ...

Transitioning with a CSS cubic-bezier function results in a noticeable jump of the content

My transition in an accordion list is using cubic-bezier(0.68, -0.55, 0.265, 1.55), but the issue I am facing is that the content below is also 'jumping'. Does anyone have suggestions on how to create space below my accordion (ul list) so that w ...

The arrangement of pictures on a card

I decided to tweak the original concept by placing the images inside a Bootstrap card. However, the end result is that the images appear to be floating. https://i.stack.imgur.com/Uf721.png Any suggestions on how to make them fit nicely within the card? ...

Navigating the Google Maps API: Implementing Scroll Zoom to Focus on a Single Marker

I'm looking for a solution for my project where the scroll zoom function will focus on zooming in on a marker or specific point rather than defaulting to the cursor. The current behavior is that the scroll zoom always centers on the cursor. Can anyone ...