There are several problems with the code. In addition to the problem with getElementByClass
(there is no such method, use document.getElementsByClassName("collapse")[0]
instead), you are trying to access the elements in the table using ChildNodes
(undefined). You probably meant to write childNodes
, but you should really using children
, since you don’t want to have to do with all the text nodes containing just whitespace. And you are accessing cells without checking that they exist; not all rows have 6th cell in your table.
There is also a spurious </tr>
tag that confuses table parsing. Remove it.
Morever, to get the content of a td
element, you cannot use nodeValue
, which is defined for text nodes only. You could use innerText
, but due to issues in browser support, the good old innerHTML
is safer. Then you need to convert the data from string to number (otherwise adding 2 and 2 gives your 22, not 4), e.g. with the Number
function. You may wish to add some error-checking here (and elsewhere).
for(var i=0;i<8;i++)
marks[i] = table.children[0].children[i+1].children[5].innerHTML;
for(var i=0;i<8;i++)
total += Number(marks[i]);