Include custom javascript in the form

Sometimes we need to have a common function that must be called from several controls. For this purpose there is a new category in the scripts section called 'Includes'. In this section you can write either javascripts that will be added in the form or change the default styles (CSS) with your desired style (See this article).

We have 3 controls a Number, a choice and a boolean (Yes/No) and their values affect the final result.

The first field is the Initial Amount(number), the second multiplies the initial amount and the third decides to double or not the final amount.First we create these 3 controls.

In the 'Includes' script section we must declare a function that calculates the final result and will be called from all 3 controls. This function's code is

Code

<script>

function CalculateResult(form) {

var result = 0;

var amount = form.GetControl("c_Amount").GetValue();

var multiply = form.GetControl("c_FirstOperator").GetValue();

if (multiply == "" || multiply =="Single")

result = 1 * parseInt(amount);

else if (multiply == "Double")

result = 2 * parseInt(amount);

else if (multiply == "Triple")

result = 3 * parseInt(amount);

if (form.GetControl("c_Double").GetValue() == "1") result = result * 2;

form.GetControl("c_Result").SetValue(result.toString());

}

</script>

Last thing to do is to go to the value change section of the 3 controls and call the above function.

The form and the calculation of the results in 3 steps (one step per control change) will be :