Getting value from table cell in JavaScript…not jQuery

后端 未结 12 1121
后悔当初
后悔当初 2020-12-02 18:19

I can\'t believe how long this has taken me but I can\'t seem to figure out how to extract a cell value from an HTML table as I iterate through the table with JavaScript. I

相关标签:
12条回答
  • 2020-12-02 18:57

    Have you tried innerHTML?

    I'd be inclined to use getElementsByTagName() to find the <tr> elements, and then on each to call it again to find the <td> elements. To get the contents, you can either use innerHTML or the appropriate (browser-specific) variation on innerText.

    0 讨论(0)
  • 2020-12-02 19:07

    If you are looking for the contents of the TD (cell), then it would simply be: col.innerHTML

    I.e: alert(col.innerHTML);

    You'll then need to parse that for any values you're looking for.

    0 讨论(0)
  • 2020-12-02 19:11

    the above guy was close but here is what you want:

           var table = document.getElementById(tableID);
           var rowCount = table.rows.length;        
             for (var r = 0, n = table.rows.length; r < n; r++) {
            for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                alert(table.rows[r].cells[c].firstChild.value);
            }
        }
            }catch(e) {
                alert(e);
            }
    
    0 讨论(0)
  • 2020-12-02 19:11

    A few problems:

    • The loop conditional in your for statements is an assignment, not a loop check, so it might infinite loop

    • You should use the item() function on those rows/cells collections, not sure if array index works on those (not actually JS arrays)

    • You should declare the row/col objects to ensure their scope is correct.

    Here is an updated example:

    var refTab=document.getElementById("ddReferences") 
    var  ttl; 
    // Loop through all rows and columns of the table and popup alert with the value 
    // /content of each cell. 
    for ( var i = 0; i<refTab.rows.length; i++ ) { 
      var row = refTab.rows.item(i); 
      for ( var j = 0; j<row.cells.length; j++ ) { 
        var col = row.cells.item(j);
        alert(col.firstChild.innerText); 
      } 
    }
    

    Replace innerText with innerHTML if you want HTML, not the text contents.

    0 讨论(0)
  • 2020-12-02 19:15

    If I understand your question correctly, you are looking for innerHTML:

    alert(col.firstChild.innerHTML);
    
    0 讨论(0)
  • 2020-12-02 19:16

    I know this is like years old post but since there is no selected answer I hope this answer may give you what you are expecting...

    if(document.getElementsByTagName){  
        var table = document.getElementById('table className');
        for (var i = 0, row; row = table.rows[i]; i++) {
        //rows would be accessed using the "row" variable assigned in the for loop
         for (var j = 0, col; col = row.cells[j]; j++) {
         //columns would be accessed using the "col" variable assigned in the for loop
            alert('col html>>'+col.innerHTML);    //Will give you the html content of the td
            alert('col>>'+col.innerText);    //Will give you the td value
            }
          }
        }
    }
    
    0 讨论(0)
提交回复
热议问题