Iterating through a table using JavaScript to get the values โ€‹โ€‹of specific cells

How can I iterate over tables 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;

     }
}

      

+3


source to share


1 answer


Use innerHTML

.

Try the following:



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);
     }
}  

      

+1


source







All Articles