How to get table row data in javascript:
Example on get table each row data using javascript:
- When we are working with HTML tables we will get a scenario like to get whole table rows data, or table td values , table cell value in JavaScript.
- For that we need to read the table by using JavaScript.
- lets see example on get table cell data using java script.
- Before that lets see how to read the data of table using JavaScript
- To find number of rows of a table by using table id in JavaScript.
- var numberOfrows=document.getElementById("tableData").rows.length;
- Get number of cells or number of tds inside each tr (table row)
- var numberoftds = document.getElementById("tableData").rows[0].cells.length;
- JavaScript get table row values get table td data
- var numberoftds = document.getElementById("tableData").rows[0].cells.item(0).innerHTML;
Example on get table each row data using javascript:
- <script type="text/javascript">
- function checkFun() {
- var n1 = document.getElementById("tableData").rows.length;
- var i=0,j=0;
- var str="";
- for(i=0; i<n1;i++){
- var n2 = document.getElementById("tableData").rows[i].cells.length;
- for(j=0; j<n2;j++){
- var x=document.getElementById("tableData").rows[i].cells.item(j).innerHTML;\
- str=str+x+":";
- }
- str=str+"#";
- }
- document.getElementById("tablecontent").innerHTML=str;
- }
- </script>
- <body onload="checkFun()">
- <table id="tableData" border="1">
- <tr>
- <td >37</td>
- <td >46</td>
- <td >3</td>
- <td >64</td>
- </tr>
- <tr>
- <td >10</td>
- <td >4</td>
- <td >7</td>
- <td >21</td>
- </tr>
- </table>
- <p id="tablecontent" ></p>
- </body>
- Here in this example on page loading we called the JavaScript function
- For that used body onload="func()" .
- Practice this example in your System.
Bagikan
How to get table cell data using JavaScript
4/
5
Oleh
Kris Kimcil