RMN RMN - 1 year ago 118
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 Source


    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++) {
             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)
(bold values are the values you will get with the code above)
I hope it helps, you only need the table id.