Get a particular cell value from HTML table using JavaScript

前端 未结 10 838
暖寄归人
暖寄归人 2020-12-15 06:09

I want to get each cell value from an HTML table using JavaScript when pressing submit button.

How to get HTML table cell values?

相关标签:
10条回答
  • 2020-12-15 06:30

    Just simply.. #sometime when larger table we can't add the id to each tr

         <table>
            <tr>
                <td>some text</td>
                <td>something</td>
            </tr>
            <tr>
                <td>Hello</td>
                <td>Hel</td>
            </tr>
        </table>
    
        <script>
            var cell = document.getElementsByTagName("td"); 
            var i = 0;
            while(cell[i] != undefined){
                alert(cell[i].innerHTML); //do some alert for test
                i++;
                }//end while
        </script>
    
    0 讨论(0)
  • 2020-12-15 06:35
    var table = document.getElementById("someTableID"); 
    var totalRows = document.getElementById("someTableID").rows.length;
    var totalCol = 3; // enter the number of columns in the table minus 1 (first column is 0 not 1)
    //To display all values
    for (var x = 0; x <= totalRows; x++)
      {
      for (var y = 0; y <= totalCol; y++)
        {
        alert(table.rows[x].cells[y].innerHTML;
        }
      }
    //To display a single cell value enter in the row number and column number under rows and cells below:
    var firstCell = table.rows[0].cells[0].innerHTML;
    alert(firstCell);
    //Note:  if you use <th> this will be row 0, so your data will start at row 1 col 0
    
    0 讨论(0)
  • 2020-12-15 06:36

    To get the text from this cell-

    <table>
        <tr id="somerow">
            <td>some text</td>            
        </tr>
    </table>
    

    You can use this -

    var Row = document.getElementById("somerow");
    var Cells = Row.getElementsByTagName("td");
    alert(Cells[0].innerText);
    
    0 讨论(0)
  • 2020-12-15 06:37

    Make a javascript function

    function addSampleTextInInputBox(message) {
        //set value in input box
        document.getElementById('textInput').value = message + "";
        //or show an alert
        //window.alert(message);
    }
    

    Then simply call in your table row button click

    <td class="center">
        <a class="btn btn-success" onclick="addSampleTextInInputBox('<?php echo $row->message; ?>')" title="Add" data-toggle="tooltip" title="Add">
            <span class="fa fa-plus"></span>
        </a>
    </td>
    
    0 讨论(0)
提交回复
热议问题