<!DOCTYPE html> <html> <head> <title></title> </head> <body> <table id="myTable"> <tr> <td>Row 1, Col 1</td> <td>Row 1, Col 2</td> </tr> <tr> <td>Row 2, Col 1</td> <td>Row 2, Col 2</td> </tr> </table> <script> var i, table = document.getElementById('myTable'), rows = table.getElementsByTagName('tr'), cells; // Iterate over each row for (i = 0; i < rows.length; i++) { // For each row, get the list of cells cells = rows[i].getElementsByTagName('td'); // Iterate over the list of cells for (j = 0; j < cells.length; j++) { // Assign a new value to each cell // For this example, I'm just prepending [i, j] (where i = row and j = col) cells[j].innerHTML = "[" + i + ", " + j + "] " + cells[j].innerHTML; } } </script> </body> </html>
|