Greetings, I am interested in creating a script that can dynamically generate code based on the number of table rows it finds. Let me explain with an example:
Let's say I have a piece of code that calculates the number of table rows (excluding the table header) when I click a button:
var rowCount = $('#items-table tr').length - 1;
What I aim to achieve is, if the count I get is 3, I want to replicate these lines of code and update the variables and numbers within them accordingly.
Here are the initial lines of code I wish to duplicate:
table = document.getElementById("items-table");
var cell1 = table.rows[1].cells[0].innerHTML;
var cell2 = table.rows[1].cells[1].innerHTML;
var cell3 = table.rows[1].cells[2].innerHTML;
The goal is to modify the variable names so they can be defined later. For instance, if the count is 3, the output should look like this:
var cell1 = table.rows[1].cells[0].innerHTML;
var cell2 = table.rows[1].cells[1].innerHTML;
var cell3 = table.rows[1].cells[2].innerHTML;
var cell4 = table.rows[2].cells[0].innerHTML;
var cell5 = table.rows[2].cells[1].innerHTML;
var cell6 = table.rows[2].cells[2].innerHTML;
var cell7 = table.rows[3].cells[0].innerHTML;
var cell8 = table.rows[3].cells[1].innerHTML;
var cell9 = table.rows[3].cells[2].innerHTML;
How can I implement this? Furthermore, I would like to create a variable that can represent all the cell variables like this:
tableData = [] tableData.append(var cell7 = etc..)
This way, I could store the table data and retrieve it using localStorage in another HTML file. Additionally, I also intend to save the individual cell variables for future use:
localStorage.setItem("item-name-1", cell1);
Any suggestions on how I could accomplish this task are welcome. Thank you in advance.
Important Note: Initially, the table on my HTML page is empty. The data is then populated using inputs and an add button.