How to get all table cell value from a table

Description

To get all table cell value on a table by using the code below

Use a loop to get the values ​​of all cells.

let myTable = document.getElementById('targetTable');

for (let row of myTable.rows) {
    for(let cell of row.cells){
       console.log(cell.innerText);
    }
}

You can also get the values ​​of all cells in the table by the following method.

let myTable = document.getElementById('targetTable');
let cells = myTable.querySelectorAll('td');
cells.forEach( (cell) =>  console.log(cell.innerHTML));

How to return the number of cells in the table’s first row.

<script>
function myFunction() {
  var x = document.getElementById("myTable").rows[0].cells.length;
  document.getElementById("demo").innerHTML = "Found " + x + " cells in the first tr element.";
}
</script>

Leave a comment

Your email address will not be published. Required fields are marked *