Is there a way to prevent the table from refreshing?

To better understand my point, please run the code snippet provided. I have 49 cells with randomized numbers that change every time a cell is clicked. My goal is to keep these numbers from changing unless the page is refreshed or a specific button is clicked.

var uniqueCell = document.getElementById('uniqueCell');

function myFunction() {
  document.querySelector("uniqueCell").style.backgroundColor = "red";
}

var isCol=0;
var board=[];
for(r=0;r<7;r++){
var line=[];
for(c=0;c<7;c++){
line.push(r);
}
board.push(line);
}


function prs(c,r){
showTable(c,r);
isCol=(isCol+1)%2;
}



function toColor(col,row,chosen_col,chosen_row){
var ret=false;
switch(isCol){
case 0:
if(row==chosen_row){
ret=true;
}
break;
case 1:
if(col==chosen_col){
ret=true;
}
break;
}

return ret;
}

function showTable(chosen_col,chosen_row){
var str="";
str+="<table border=1>";
for(row=0;row<7;row++){
str+="<tr>";
for(col=0;col<7;col++){
str+="<td onclick='prs("+col+","+row+")'";
if(toColor(col,row,chosen_col,chosen_row)){
str+=" class='grn' ";
}
str+=">";
str+=RandomGenerator(50, 500);
str+="</td>";
}
str+="</tr>";
}
str+="</table>";

 document.getElementById("ff").innerHTML=str;
}



function RandomGenerator(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}



showTable(-1);
td{
border:2px solid black;
width:10px;
height:10px;
}
td:hover{background-color:lightgreen;}
.grn{
background-color:green;
color:white;
}
<div id='ff'></div>
<td id = "uniqueCell">  </td>

Answer №1

If you already possess the matrix called board, you have the option to populate it with random numbers and subsequently display the table.

var uniqueCell = document.getElementById("uniqueCell");

function myFunction() {
  document.querySelector("uniqueCell").style.backgroundColor = "red";
}

var isCol = 0;
var board = [];
for (r = 0; r < 7; r++) {
  var line = [];
  for (c = 0; c < 7; c++) {
    line.push(RandomGenerator(50, 500));
  }
  board.push(line);
}

function prs(c, r) {
  showTable(c, r);
  isCol = (isCol + 1) % 2;
}

function toColor(col, row, chosen_col, chosen_row) {
  var ret = false;
  switch (isCol) {
    case 0:
      if (row == chosen_row) {
        ret = true;
      }
      break;
    case 1:
      if (col == chosen_col) {
        ret = true;
      }
      break;
  }

  return ret;
}

function showTable(chosen_col, chosen_row) {
  var str = "";
  str += "<table border=1>";
  for (row = 0; row < 7; row++) {
    str += "<tr>";
    for (col = 0; col < 7; col++) {
      str += "<td onclick='prs(" + col + "," + row + ")'";
      if (toColor(col, row, chosen_col, chosen_row)) {
        str += " class='grn' ";
      }
      str += ">";
      str += board[row][col];
      str += "</td>";
    }
    str += "</tr>";
  }
  str += "</table>";

  document.getElementById("ff").innerHTML = str;
}

function RandomGenerator(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

showTable(-1);
td {
  border: 2px solid black;
  width: 10px;
  height: 10px;
}
td:hover {
  background-color: lightgreen;
}
.grn {
  background-color: green;
  color: white;
}
If you already have the `board` matrix, you can populate it with random values and then visualize a table.
<div id="ff"></div>
<td id="uniqueCell"></td>

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

The file field appears to be empty when sending a multipart/form-data request via AJAX

I'm encountering an issue when attempting to submit a multipart/form-data using AJAX. Here is the HTML code snippet: <form id='form_foto' method='post' enctype='multipart/form-data'> <input type='hidden ...

Removing an object from a JSON array based on checkbox selection in Angular 4

0: CategoryId: "31b7a227-9fda-4d14-8e1f-1dee5beeccb4" Code: "GMA0300" Description: "PA-5215: Renamed" Enabled: true Favorite: false Id: "26cfdb68-ef69-4df0-b4dc-5b9c6501b0dd" InstrumentType: null Moniker: "1GMA0300" Name: "Celiac Disease Panel (tTG IgG, tT ...

Ways to send data to nested elements when implementing secure routes?

I am currently working on a project to develop a 'Train Ticket Reservation System' using ReactJS. In order to access the services, users must login, so I have implemented protected routes to render certain components. Instead of relying on the de ...

Issue: ray.intersectScene does not exist as a function

For my basic WebGL project, I have been utilizing the sim.js code and its components. Recently, when I attempted to use the frustum class (refer to this question), it required updating my three.js. Unfortunately, this caused an issue: TypeError: ray.inter ...

Which specific CSS rule is responsible for the issue I am currently experiencing?

I have styled a left-hand border on items in a list, with the border-left-color set to transparent for all of them. The active item, marked by the css class "active day", has a specific color for its border. Below is a snippet of the code (certain styles h ...

``Can you provide step-by-step instructions on how to delete a particular item from

Currently, I am in the process of developing a simple comment list application using JavaScript and local storage. I have completed all the necessary details, but now I need to figure out how to remove a specific item that was created by the application ...

Ensuring the safety of PHP JSON output results on a web server

I am currently developing an app using phonegap that submits and retrieves data from a MySQL database hosted on a server (website). I have successfully implemented the data submission and retrieval features in the app. The data is fetched through AJAX fro ...

Retrieving information from a mongoDB query in a nodejs environment

I recently started learning the MEAN stack and encountered an issue. I need assistance in sending the following query data to the frontend. router.get('/average', (req, res) => { Employees.aggregate([ { $match: { "position": "sen" } }, ...

Guide on implementing a confirmation dialog box for updating records in the jQuery Kendo UI Grid

When using Kendo UI, it prompts a confirmation window before deleting a record. https://i.sstatic.net/r2fti.png Is there a way to add this feature to the update and add record buttons? An example is provided below that demonstrates hooking all callback f ...

Adjust the autofocus to activate once the select option has been chosen

Is there a way to automatically move the cursor after selecting an option from a form select? <select name="id" class="form-control"> <option>1</option> <option>2</option> <option>3</option&g ...

Determine the frequency of a specific key in an array of objects

original array: ................ [ { from: {_id: "60dd7c7950d9e01088e438e0"} }, { from: {_id: "60dd7c7950d9e01088e438e0"} }, { from: {_id: "60dd7e19e6b26621247a35cd"} } ] A new array is created to count the instances of each ...

Guide on accessing an array within a JSON object?

I have the following JSON object: [ { "comments": [ { "created_at": "2011-02-09T14:42:42-08:00", "thumb": "xxxxxxx", "level" ...

Encountering a "Unable to use import statement outside a module" issue when trying to import react-hook-mousetrap within a Next.js project

Currently experimenting with Next.js but encountering some challenges. Recently attempted to add react-hook-mousetrap and imported it as per usual: import useMousetrap from "react-hook-mousetrap"; However, this resulted in the following error: S ...

Queueing in jQuery on a selected group of elements

I have a group of images inside a div that are tagged with different class names for filtering purposes. I am attempting to create a sequence of effects (simple animated show/hide) on these images. <div id="myDiv"> <img class="tag1 tag2 tag3" ...

There was a problem with the WebSocket handshake: the response header value for 'Sec-WebSocket-Protocol' did not match any of the values sent

I've encountered an issue with my React project that involves streaming live video through a WebSocket. Whenever the camera firmware is updated, I face an error in establishing the WebSocket connection. Here's how I initiate the WebSocket: wsRe ...

Using HTML and JavaScript to add variables into the URL within the window.location

I have been struggling to incorporate longitude and latitude into the URL. Despite researching various topics online, I have not found a solution that works for me. Below is the HTML code that showcases the issue. When you click the "Show Position" button ...

Display a window.confirm alert to prevent any unauthorized changes to the URL

Looking to implement a feature that prevents changing the URL from my component. The goal is to display a message and allow users to choose between canceling (no change to URL) or confirming (change the URL). How can I achieve this in the upcoming 13.4 ver ...

Creating a cutout effect on 3D text within a Geometry using Three.js

I am looking to convert my 3D text into Geometry so that I can utilize it with CSG (I have also used a Three.js CSG wrapper) in order to subtract it from another object, similar to the scenario described in this question. Here is my 3D text: loader.load( ...

Issue with jQuery Ajax file upload in CodeIgniter

I am attempting to use AJAX to upload a file in the CodeIgniter framework, but I encountered an error message stating 'You did not select a file to upload.' Please review this code: View <form method="POST" action="" enctype="multipart/form- ...

How can I style the inner div by adding a class to the first div?

In my project, I have a list of elements that are generated dynamically, all styled the same way but with different content. The first element has a specific styling, and if it doesn't render, I want the second element to inherit that styling. < ...