Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Int not executing when entered in the loop

Parse Int not executed. I can not figure out why this issue is arising.

function compute(){
    var totalDis = document.getElementById("totaldisplay");
    var htmlDis = "";
    htmlDis+="<table><tr><th>Student Name</th><th>Total Mark</th>";
    for(var row=0;row<names.length;row++)
    {
        var valMrk = document.querySelectorAll("#row" + row + ".stdMrk");
        var totalval = 0;
        for(var i=0;i<=5;i++)
        {
            totalval += parseInt(valMrk[i].Value);
            //alert("testing compute function");
        }   
        htmlDis += "<tr id='row" + i +"'><td style='border:1px solid black;'>"+names[row]+"</td><td style='border:1px solid black;'>"+totalval+"</td>";

    }
    htmlDis+="</table>";
    totalDis.innerHTML=htmlDis;
}

According to the code above, the function stops executing when the function enters the loop and stops at the parse int step, which is annoying because, on another form, it works fine with the same code with just a few modifications of variables.

Why is this issue arising?

(Edited according to answer) this is the loop that generate the input box and the above codes is supposed to compute each row but after making the changes, the row is displaying but not computing, only displaying 0 for each row.

Here are the codes for the loop generating the textboxes.

for(j=0;j<names.length;j++)
    {
        html+="<tr><td style='border:1px solid grey;'>"+names[j]+"</td>";
        for(i = 0; i<num_of_ica; i++)
        {

            html+="<td><input type='text' class='stdMrk' placeholder='0' value='0' /></td>";
        }
        for(var k=1;k<=1;k++)//display project or final exams txt box
        {
            html+= "<td><input type='text' class='stdMrk' placeholder='0' value='0' /></td>";

        }

        html+="</tr>";
    }
like image 890
Krishna Ramdin Avatar asked Jul 25 '26 08:07

Krishna Ramdin


1 Answers

Obviously valMrk.length is less than 5 so in the loop it reaches to index that does not exist and it generates error. change it like this (also make sure that all values can be parsed as int):

var valMrk = document.querySelectorAll("#row" + row + ".stdMrk");
    var totalval = 0;
    for(var i=0;i<valMrk.length;i++)
    {
        if(!isNaN(valMrk[i].Value)
        totalval += parseInt(valMrk[i].Value);
        //alert("testing compute function");
    }   
like image 83
Ashkan Mobayen Khiabani Avatar answered Jul 27 '26 22:07

Ashkan Mobayen Khiabani