How do I iterate through table rows and cells in JavaScript?

前端 未结 9 919
旧时难觅i
旧时难觅i 2020-11-22 13:05

If I have an HTML table...say

<
相关标签:
9条回答
  • 2020-11-22 13:20

    var table=document.getElementById("mytab1");
    var r=0; //start counting rows in table
    while(row=table.rows[r++])
    {
      var c=0; //start counting columns in row
      while(cell=row.cells[c++])
      {
        cell.innerHTML='[R'+r+'C'+c+']'; // do sth with cell
      }
    }
    <table id="mytab1">
      <tr>
        <td>A1</td><td>A2</td><td>A3</td>
      </tr>
      <tr>
        <td>B1</td><td>B2</td><td>B3</td>
      </tr>
      <tr>
        <td>C1</td><td>C2</td><td>C3</td>
      </tr>
    </table>

    In each pass through while loop r/c iterator increases and new row/cell object from collection is assigned to row/cell variables. When there's no more rows/cells in collection, false is assigned to row/cell variable and iteration through while loop stops (exits).

    0 讨论(0)
  • 2020-11-22 13:23

    Using a single for loop:

    var table = document.getElementById('tableID');  
    var count = table.rows.length;  
    for(var i=0; i<count; i++) {    
        console.log(table.rows[i]);    
    }
    
    0 讨论(0)
  • This solution worked perfectly for me

    var table = document.getElementById("myTable").rows;
    var y;
    for(i = 0; i < # of rows; i++)
    {    for(j = 0; j < # of columns; j++)
         {
             y = table[i].cells;
             //do something with cells in a row
             y[j].innerHTML = "";
         }
    }
    
    0 讨论(0)
  • 2020-11-22 13:28

    You can use .querySelectorAll() to select all td elements, then loop over these with .forEach(). Their values can be retrieved with .innerHTML:

    const cells = document.querySelectorAll('td');
    cells.forEach(function(cell) {
      console.log(cell.innerHTML);
    })
    <table name="mytab" id="mytab1">
      <tr> 
        <td>col1 Val1</td>
        <td>col2 Val2</td>
      </tr>
      <tr>
        <td>col1 Val3</td>
        <td>col2 Val4</td>
      </tr>
    </table>

    If you want to only select columns from a specific row, you can make use of the pseudo-class :nth-child() to select a specific tr, optionally in conjunction with the child combinator (>) (which can be useful if you have a table within a table):

    const cells = document.querySelectorAll('tr:nth-child(2) > td');
    cells.forEach(function(cell) {
      console.log(cell.innerHTML);
    })
    <table name="mytab" id="mytab1">
      <tr> 
        <td>col1 Val1</td>
        <td>col2 Val2</td>
      </tr>
      <tr>
        <td>col1 Val3</td>
        <td>col2 Val4</td>
      </tr>
    </table>

    0 讨论(0)
  • 2020-11-22 13:31

    If you want to go through each row(<tr>), knowing/identifying the row(<tr>), and iterate through each column(<td>) of each row(<tr>), then this is the way to go.

    var table = document.getElementById("mytab1");
    for (var i = 0, row; row = table.rows[i]; i++) {
       //iterate through rows
       //rows would be accessed using the "row" variable assigned in the for loop
       for (var j = 0, col; col = row.cells[j]; j++) {
         //iterate through columns
         //columns would be accessed using the "col" variable assigned in the for loop
       }  
    }
    

    If you just want to go through the cells(<td>), ignoring which row you're on, then this is the way to go.

    var table = document.getElementById("mytab1");
    for (var i = 0, cell; cell = table.cells[i]; i++) {
         //iterate through cells
         //cells would be accessed using the "cell" variable assigned in the for loop
    }
    
    0 讨论(0)
  • 2020-11-22 13:31

    You can consider using jQuery. With jQuery it's super-easy and might look like this:

    $('#mytab1 tr').each(function(){
        $(this).find('td').each(function(){
            //do your stuff, you can use $(this) to get current cell
        })
    })
    
    0 讨论(0)
提交回复
热议问题
col1 Val1