CalcBuilder Forum

Javascript events cb_setValue

User reedjeffry 2015-11-04 11:20:24

HI,

I am not understanding Javascript events: cb_getValue and cb_setValue, or at least not observing what I expected. I need to do calculations, but just for testing purposes, consider

Fields:
averageSalary (Type: Number)
benefits (Type: Number)
annualSalary (Type: Number)
monthlySalary (Type: Number)

Javascript events, Executed before calculate execution

//Get Value from Field
//var result=cb_getValue('myvar');
var averageSalaryResult=cb_getValue('averageSalary');
var benefitsResult=cb_getValue('benefits');

//Set Value from Field
//cb_setValue('myvar','NEW_VALUE');
cb_setValue('annualSalary','averageSalaryResult');
cb_setValue('monthlySalary','benefitsResult');

When I enter a number in averageSalary and benefits, I expected to see the numbers in fields annualSalary and monthlySalary. However, the text "averageSalaryResult" and "benefitsResult" is shown.

Moonsoft support 2015-11-04 13:23:40

Hi,
at your code you're getting the values of the two fields, and then setting a new values for the other fields. As you're using

cb_setValue('annualSalary','averageSalaryResult');

you're setting a fixed text 'averageSalaryResult' to that input. In case you want to use the value you have just recovered from the other field, use without '':

cb_setValue('annualSalary',averageSalaryResult);


For setting 'result' values inside the input form, you can follow other different approach as well, using inline fields:

- Change the type of the destination fields from 'text' to 'inline result'
- Now at the code section (the main php code, not the js tabs), you can set your calculated value to that fields, for ex:

$annualSalary=$averageSalaryResult;


This will change 'annualSalary' value each time user changes values at the form.


Hope this helps, please let us know if you need further help with this.


Regards

...
Support/development

Perfect for small code changes or to correct any bug at your site

Buy now!
...
Support/development 40 hours

With the peace of mind of having a professional team at your service (20% discount)

Buy now!