Client Script with parseInt

SnowDevOps
Giga Guru

Hello

I have the simple form here to calculate the total number of grade. All the fields below are string. I'm not sure why the total field is still saying "NaN". My client script seemed to be correct. Thanks 

 

SnowDevOps_0-1731694856292.png

My client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    if (newValue) {
        var t = parseInt(g_form.getValue('telugu'));
        var h = parseInt(g_form.getValue('hindi'));
        var e = parseInt(g_form.getValue('english'));
        var m = parseInt(g_form.getValue('math'));
        var sc = parseInt(g_form.getValue('science'));
        var s = parseInt(g_form.getValue('social'));
        var tl = parseInt(t+h+e+m+sc+s);
        g_form.setValue('total', tl);
    }

}

 

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

One of the field/variable names must be incorrect.  Alert on each script variable to find the culprit.

You're right, it's field typo. 

Juhi Poddar
Kilo Patron

Hello @SnowDevOps 

  • Which field have you set onChange Client script 
  • Updated Script:

 

 if (newValue) {
        var t = parseInt(g_form.getValue('telugu'));
        var h = parseInt(g_form.getValue('hindi'));
        var e = parseInt(g_form.getValue('english'));
        var m = parseInt(g_form.getValue('math'));
        var sc = parseInt(g_form.getValue('science'));
        var s = parseInt(g_form.getValue('social'));
        var tl = parseInt(t+h+e+m+sc+s);
        g_form.setValue('total', tl.toString()); //tl is set back to string type and ten set the value
    }​

 

Note: tl needs to be set back to string before it's value is set to a field.

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar