Sometimes, we want to get the value from a table cell in JavaScript.
In this article, we’ll look at how to get the value from a table cell in JavaScript.
How to get the value from a table cell in JavaScript?
To get the value from a table cell in JavaScript, we loop through the table rows and columns.
For instance, we write
const getCellValues = () => {
const table = document.getElementById("mytable");
for (const row of table.rows) {
for (const cell of row.cells) {
console.log(cell.innerHTML);
}
}
};
to define the getCellValues function.
In it, we get the table element with getElementById.
Next, we use a for-of loop to loop through the table.rows table rows.
In it, we loop through the cells in row.cells.
Then we get the cell’s value with the cell.innerHTML property.
Conclusion
To get the value from a table cell in JavaScript, we loop through the table rows and columns.