loop through all td elements in a table javascript code example

Example 1: Javascript looping through table

var table = document.getElementById("myTable");
for (let i in table.rows) {
   let row = table.rows[i]
   //iterate through rows
   //rows would be accessed using the "row" variable assigned in the for loop
   for (let j in row.cells) {
     let col = row.cells[j]
     //iterate through columns
     //columns would be accessed using the "col" variable assigned in the for loop
   }  
}

Example 2: loop trough table rows

var table = document.getElementById("hoursContainer");
 	//iterate trough rows
    for (var i = 0, row; row = table.rows[i]; i++) {
 	//iterate trough columns
        for (var j = 0, col; col = row.cells[j]; j++) {
           // do something
           }
        }
    }

Tags:

Misc Example