JavaScript - Loop Through Table Cells using JS Last Updated : 02 Dec, 2024 Comments Improve Suggest changes Like Article Like Report To loop through table cells in JavaScript, you can use various methods to iterate over the rows and cells in an HTML table. This allows you to manipulate or retrieve data from each cell as needed. Below are some practical approaches.1. Using the for LoopThe for loop is a simple and traditional way to iterate through table rows and cells. The for loop iterates through rows and cells, giving you control over the loop and access to each cell's content. HTML <!DOCTYPE html> <html lang="en"> <body> <table id="myTable" border="1"> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> </tr> </table> <button onclick="loopThroughTable()">Loop Through Table Cells</button> <script> function loopThroughTable() { let table = document.getElementById("myTable"); for (let i = 0; i < table.rows.length; i++) { // Loop through rows let row = table.rows[i]; // Loop through cells in each row for (let j = 0; j < row.cells.length; j++) { // Change background color of cell row.cells[j].style.backgroundColor = "yellow"; } } } </script> </body> </html> 2. Using forEach Loop The forEach method allows you to iterate over table cells in a more functional and concise way. forEach iterates over the selected table cells, providing a cleaner syntax for logging cell content. HTML <!DOCTYPE html> <html lang="en"> <body> <table id="myTable" border="1"> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> </tr> </table> <button onclick="loopThroughCells()">Loop Through Table Cells</button> <script> function loopThroughCells() { let cells = document.querySelectorAll('td'); cells.forEach(cell => { cell.style.backgroundColor = "green"; }); } </script> </body> </html> 3. Using Array.from()Array.from() allows you to convert the rows or cells collection into an array, making it easy to use array methods like forEach to iterate over the cells. This method simplifies the iteration process by turning the HTMLCollection into a standard array. HTML <!DOCTYPE html> <html lang="en"> <body> <table id="myTable" border="1"> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> </tr> </table> <button onclick="loopThroughTableRows()">Loop Through Table Rows and Cells</button> <script> function loopThroughTableRows() { let table = document.getElementById("myTable"); Array.from(table.rows).forEach(row => { Array.from(row.cells).forEach(cell => { cell.style.backgroundColor = "red"; }); }); } </script> </body> </html> ConclusionTo loop through table cells in JavaScript, you can use the traditional for loop for full control, forEach for a more concise and functional approach, or Array.from() to convert collections into arrays for easier manipulation. Each method offers flexibility based on your needs. Comment More infoAdvertise with us Next Article JavaScript - Loop Through Table Cells using JS A anujpaz9pe Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Remove Column from HTML Table using JavaScript ? Given an HTML table and the task is to remove the certain column from the HTML table. There are two approaches that are discussed below: Approach 1: First, select the table and also get the rows of table using table.rows. Get the number of columns of a row and go through each one of the columns. Use 3 min read How to export HTML table to CSV using JavaScript ? Comma Separated Values or CSV is a type of text file where each value is delimited by a comma. CSV files are very useful for the import and export of data to other software applications.Sometimes while developing web applications, you may come into a scenario where you need to download a CSV file co 6 min read How to Convert HTML Table to JSON in JavaScript? HTML tables are commonly used to present structured data on websites. In many scenarios, this tabular data needs to be converted to JSON format for processing, storage, or server communication. We will discuss different approaches to converting HTML tables to JSON using JavaScript.These are the foll 3 min read How to Access <tr> element from Table using JavaScript ? Given an HTML table and the task is to access the table element from the Controller and highlight any row that we want. Approach: We will use a basic DOM operation in JavaScript to access table row element. We will be adding highlight class to the row that we click, if the highlight class is already 2 min read How to convert JSON data to a html table using JavaScript/jQuery ? To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works.Table of ContentUsing for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a v 4 min read JavaScript - How to Sort Rows in a Table? Rows in a table are horizontal sections that hold data, organizing information across columns in a structured, easy-to-read format. To sort table rows in JavaScript, create a function that compares rows based on cell values. Use a loop to check and swap rows as needed until all rows are in order.App 4 min read How to loop through HTML elements without using forEach() loop in JavaScript ? In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Approach 1: Using the for loopApproach 2: Using the While loopApproach 3: Using the 'for.....of' statementApproach 4: Using the for...in state 4 min read How to Create an HTML Table from an Object Array Using JavaScript ? Tables are a fundamental part of web development, and displaying data in a structured manner is a common requirement. JavaScript provides a powerful way to dynamically generate HTML content, making it easy to create tables from object arrays. Table of Content Using innerHTML propertyUsing appendChil 2 min read How to Create a Filter Table with JavaScript? Filter tables are commonly used in web applications to organize and display tabular data efficiently. It allows users to search and filter through large datasets easily. In this tutorial, we will go through the steps to create a filter table with JavaScript. ApproachFirst, create the basic HTML stru 3 min read How to dynamically insert id into table element using JavaScript ? This article explains how to dynamically insert "id" into the table element. This can be done by simply looping over the tables and adding "id"s dynamically. Below are the approaches used to dynamically insert id into table elements using JavaScript: Table of Content Using classList ObjectUsing clas 3 min read Like