Client Script with parseInt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 10:24 AM
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
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 10:31 AM
One of the field/variable names must be incorrect. Alert on each script variable to find the culprit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 10:35 AM
You're right, it's field typo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 10:42 AM
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