I'm trying to store a table in a 2d array so that each item in the array will be an array containing all the cells in a row.
I'm using:
var tRow = [];
var tRows = [];
tab = document.getElementById('table');
for(var r = 0 ; r < tab.rows.length ; r++) {
for (var c=0; c < tab.rows[r].cells.length; c++){
tRow[c] = tab.rows[r].cells[c].innerHTML;
}
tRows.push(tRow);
}
this just gives me the last row item in 20 places rather than each item in the table at its respective index. So, for this table:
<tr>
<td>Row 1 cell 1</td>
<td>Row 1 cell 2</td>
<td>Row 1 cell 3</td>
<td>Row 1 cell 4</td>
</tr>
<tr>
<td>Row 2 cell 1</td>
<td>Row 2 cell 2</td>
<td>Row 2 cell 3</td>
<td>Row 2 cell 4</td>
</tr>
tRows will be:
tRows =[ [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] , [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] ]
instead of:
tRows =[ [Row 1 cell 1,Row 1 cell 2,Row 1 cell 3,Row 1 cell 4] , [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] ]
I don't know what I'm doing wrong. Please help.