RMN RMN - 6 months ago 49
jQuery Question

Get Cell Value using javascript

I have an asp:table(table1), in a user control with following structure




| Col2 | Col3 | SUM
DEFAULT | 5 | 2 |
R1 | 1 | 2 |
R2 | 4 | 5 |





Here only headers are created statically in aspx page, rest all rows are added in .vb(dynamically) page.

My requirement is, on page load(in client side, after table gets loaded) I want the Sum column to show sum of Col2 and Col3. Also since the Col2 and Col3 are editable, on data change in cells, the Sum values should get updated itself.

How can I find the cells, since I dont have their ID, also which events should be called to show these values ?

Answer

JavaScript:

    var col2,col3;
    var table = document.getElementById('tableid');
    for (var i = 0; i < table.rows.length; i++) {
        //2 - first cell number, 3 2nd cell number.
        for (var j = 2; j <= 3; j++) {
           if(j==2){//coll2
             col1 += table.rows[i].cells[j].innerHTML;
           }else if(j==3){//col3
             col2 += table.rows[i].cells[j].innerHTML;
           }
        }
    }

You can view it as so: (maybe this will make it look simple)
Table:
{row:0,cell:0},{row:0,cell:1},{row:0,cell:2},{row:0,cell:3},{row:0,cell:4}
{row:1,cell:0},{row:1,cell:1},{row:1,cell:2},{row:1,cell:3},{row:1,cell:4}
{row:2,cell:0},{row:2,cell:1},{row:2,cell:2},{row:2,cell:3},{row:2,cell:4}
...
(bold values are the values you will get with the code above)
I hope it helps, you only need the table id.

Comments