Categories
JavaScript Answers

How to get the value from a table cell in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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