Let’s say you have 2 fields, and you want the value of field 2 to depend on field 1. How you do this depends on whether the value can be expressed as a calculation.
Option 1 — Calculated fields (PRO), for values you can compute
If the value can be computed from other fields, add a calculated field. It recalculates automatically as the form is filled in, with no custom code. For example, a total based on a quantity and a price:
[number quantity]
[number price]
[calculated total formula:"[quantity] * [price]"]
You can show the result as text (the default), as a read-only input, or keep it hidden and only use it in the email. Reference the value in your mail template with the mail-tag [total]:
[calculated total formula:"[quantity] * [price]" display:hidden]
Option 2 — Give each choice its own value (PRO)
If “changing a value” really means each option of a dropdown, radio or checkbox should be worth a certain amount, use the calc_select, calc_radio or calc_checkbox tags and add =>value after each option:
[calc_select plan "Basic=>10" "Pro=>25" "Enterprise=>100"]
[calculated price formula:"[plan]"]
Now choosing “Pro” sets [price] to 25.
Option 3 — Custom JavaScript, for non-numeric values
If the new value is text rather than a number, a formula can’t express it, so a small piece of JavaScript is still the simplest solution. Say you want this logic:
- If field 1 has the value “X”, then field 2 should get the value “Y”
- If field 1 has the value “Y”, then field 2 should get the value “Z”
You could add this at the bottom of your form code:
<script>
(function($){
const $field1 = $('#field1');
const $field2 = $('#field2');
$field1.on('change',function(){
if ($field1.val() == 'X'){ $field2.val('Y'); }
if ($field1.val() == 'Y'){ $field2.val('Z'); }
});
})(jQuery);
</script>
Prefer to keep the logic out of your form markup? A calculated field also has an advanced “custom JavaScript function” mode. With [calculated field2 func:"fromField1"], your global fromField1(fields) function can read fields.get('field1') and return any value — including text.
Related issues: