Summing up two text fields dynamically
Trigger type: JavaScript Action type: Value
This advanced logic sums up two text fields, based on the condition that the user interacts with the corresponding text fields.
At runtime, if the business user interacts with text fields A and B, the Total field gets automatically filled in, displaying the sum of A and B.
-
Open or create a Form file in Studio.
-
Add three Text Field components into your form.
-
Name two of the text field components as
AandB, and name the third one asTotal. Pay attention to their Property Names, as you use those in your advanced logic.Optionally, you can set the
Totaltext field component as Disabled, so business users cannot interact with it. -
Go to the Logic tab of the
Totalfield component. -
Add a logic and name it
"dynamic sum". -
Select the Javascript trigger type, and enter the following script in the Text Area section:
result = true;
return result;The above snippet detects if there are any changes in the text fields, i.e., the user interacts with the text fields, and triggers the action to sum up the fields.
-
Add an action and name it
"a+b". -
Select the Value action type, and enter the following script in the Value (Javascript) section:
result = (+data.a) + (+data.b);
return result;Replace
aandbwith the field keys values of the text field components. -
Save the action and the logic.
-
Save the component.