Search code examples
javascripthtmlcellrows

Google Chrome sends an error of undefined cells in Javascript


for (var i = RN; i > 1; i--) {
    Credit[i] = document.getElementById("table").rows[i].cells[1].innerHTML;
    Grade[i] = document.getElementById("table").rows[i].cells[2].innerHTML;
    total += Credit[i];
}

error:Uncaught TypeError: undefined is not a function

I'm not sure why cells in undefined here.


Solution

  • My guess is you have the number of rows and you are forgetting that index starts at ZERO so you need to subtract one from the length. And you probbaly want to change the i > 1 to zero if you are skipping the first row.

    for (var i = RN-1; i > 0; i--) {