JavaScript - Loop Through Table Cells using JS Last Updated : 02 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 How to Access element from Table using JavaScript ? 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 Like We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It ! Improvement Suggest changes Suggest Changes Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal. Create Improvement Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. Suggest Changes min 4 words, max Words Limit:1000 Thank You! Your suggestions are valuable to us. What kind of Experience do you want to share? Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences