最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Iterate through a table using JavaScript to get values of specific cells - Stack Overflow

programmeradmin0浏览0评论

How can I iterate through a table using JavaScript to get the values of cells 4 and 5?

This is what I have:

function constructString() {    
    var table=document.getElementById('wcList');

    for(var i=0; i<table.rows.length;i++){

        var row       = getElem(RowId + i);
        var str_wc    = table.rows[i].cells[1].firstChild.value;
        var str_act   = table.rows[i].cells[5].firstChild.value;
        var str_coh   = table.rows[i].cells[6].firstChild.value;
        var str_mconf = table.rows[i].cells[7].firstChild.value;

        var string1 = str_wc + row + str_act + row + str_coh + row + str_mconf + row;

     }
}

How can I iterate through a table using JavaScript to get the values of cells 4 and 5?

This is what I have:

function constructString() {    
    var table=document.getElementById('wcList');

    for(var i=0; i<table.rows.length;i++){

        var row       = getElem(RowId + i);
        var str_wc    = table.rows[i].cells[1].firstChild.value;
        var str_act   = table.rows[i].cells[5].firstChild.value;
        var str_coh   = table.rows[i].cells[6].firstChild.value;
        var str_mconf = table.rows[i].cells[7].firstChild.value;

        var string1 = str_wc + row + str_act + row + str_coh + row + str_mconf + row;

     }
}
Share Improve this question edited Mar 7, 2014 at 19:19 Robbie JW 7391 gold badge9 silver badges22 bronze badges asked Jan 29, 2013 at 16:16 user2022359user2022359 311 gold badge1 silver badge2 bronze badges 3
  • 1 getElem() is not native JS or DOM function, what does it do? Also a small snippet of the HTML code would be helpful... – Teemu Commented Jan 29, 2013 at 16:18
  • working in sap mii so the page is displayed in xsl. Display table allow user to select 1 or several row and update columns 5, 6, 7 then click save button. need to Loop through all edited rows and build a string of concatenated values. Then Create a BLS (updateWCData) which takes the above created string as a input – user2022359 Commented Jan 29, 2013 at 16:34
  • Please show us one row of the table, so we don't need to guess how you can get the content you want. Just help us to help you... – Teemu Commented Jan 29, 2013 at 16:39
Add a ment  | 

1 Answer 1

Reset to default 1

Use innerHTML.

Try this:

function constructString() {   
    var table=document.getElementById('wcList');

    for(var i=0; i<table.rows.length;i++){

        // FIX THIS
        var row = 0;

        var str_wc=(table.rows[i].cells[1].innerHTML);
        var str_act=(table.rows[i].cells[5].innerHTML);
        var str_coh=(table.rows[i].cells[6].innerHTML);
        var str_mconf=(table.rows[i].cells[7].innerHTML);

        var string1=str_wc +row+str_act+row+str_coh+row+str_mconf+row;

        alert(string1);
     }
}  
发布评论

评论列表(0)

  1. 暂无评论