Client Script for adding values from 3 fields and populating it on other field

sachinshivakuma
Mega Contributor

Hi,

I need some help scripting on adding values from 3 fields and populating them in a new field.

Thanks in advance.

19 REPLIES 19

Thanks Chuck, helpful.

Sharique Azim
Mega Sage

Hi Sachin,


Although your requirement is not clear , but I would answer to best of my understandings.



store the values of the fields in some variable,


var xyz=''; // empty string


var a=g_form.getValue('field_1');


var b=g_form.getValue('field_2');


var c=g_form.getValue('field_3');


xyz +=a+b+c;


vg_form.setValue('field_4',xyz);



I hope this helps.



Regards,


Shariq


Hi Shariq,

 

    I'm using the same script to calculate the value of a+b and get total sum value in the single line text box. This script is not working for me. can you help me

 

I'm using 2 select box'es 

a Select Box has option yes or no  (Yes value as 10 ; No Value as 5)

b Select Box has option yes or no (Yes value as 10 ; No Value as 5)

 

I'm using C as a single line text box I need to get the score of what the user selects.

If Select Yes in A; Yes in B; 

It should autopopulate C as 10

 

Please help

 

Thank you!!

Jim Coyne
Kilo Patron

I'm assuming you want to update the total field each time one of the 3 fields changes, correct?   This is how you can set things up for the least maintenance:



1. an onLoad Client Script that will declare a function that does the actual calculation


function onLoad() {


    //nothing happens here, but we still need the onLoad block for this to work


}



function u_calculateTotal(){


  g_form.setValue("u_total", g_form.getIntValue("u_integer1") + g_form.getIntValue("u_integer2") + g_form.getIntValue("u_integer3"));


}



The getIntValue() function retrieves the value of the input fields as integers so no need to convert them.



2. onChange Client Scripts for each of the 3 input fields


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading) {


      return;


  }


  //call the shared function to update the total field


  u_calculateTotal();


}


Thanks Jim! I learned something new today. I wasn't acquainted with g_form.getIntValue(). Good to know.