Saturday, 15 July 2017

Table read with Java Script


This is how I consummate reading a table in javascript. Basically I study down into the rows and then I was able to drill down into the individual cells for each row. This should give you an idea.

Roll Name City
101 Sona Varanasi
102 Mona Chandauli
103 Amit Jaunpur





<html>
<head>
    <title>Girfa Student Help : Table read with javascript</title>
    <script>
        function GetData()
        {

            var oTable = document.getElementById('tbData');
            var label = document.getElementById('s1');
            //gets rows of table
            var rowLength = oTable.rows.length;

            //loops through rows   
            for (i = 0; i < rowLength; i++) {

                //gets cells of current row
                var oCells = oTable.rows.item(i).cells;

                //gets amount of cells of current row
                var cellLength = oCells.length;

                //loops through each cell in current row
                for (var j = 0; j < cellLength; j++) {
                    /* get your cell info here */
                     label.innerHTML+= oCells.item(j).innerHTML + " | ";
                }
                label.innerHTML+="<br>"
            }
        }
    </script>
</head>
<body>
    <h1 align="center">Table Data Read With Java Script</h1><hr />
    <table border="1" id="tbData" cellspacing="0" cellpadding="5">
        <tr>
            <th>Roll</th>
            <th>Name</th>
            <th>City</th>
        </tr>
        <tr>
            <td>101</td>
            <td>Sona</td>
            <td>Varanasi</td>
        </tr>
        <tr>
            <td>102</td>
            <td>Mona</td>
            <td>Chandauli</td>
        </tr>
        <tr>
            <td>103</td>
            <td>Amit</td>
            <td>Jaunpur</td>
        </tr>
    </table><br />
    <input type="button" value="Read" onclick="GetData()" /><br /><br />
    <span id="s1"></span>
</body>

</html>