Capture Calculation Result as Text Field Value

Instructions

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/

Code

Filename: gpac-calculation-on-text-field.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Gravity Perks // GP Advanced Calculations // Capture Calculation Result as Text Field Value
 * https://gravitywiz.com/documentation/gravity-forms-advanced-calculations
 *
 * Instruction Video: https://www.loom.com/share/db50e407e0634748a36b189b63e0bb92
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 */
// Update these variables to match your calculation and target text field ID
var calc_field_id   = 4;
var target_field_id = 3;

// Update this config map for calculation results to text values
var config_map = {
	1: 'Tiny',
	2: 'Medium',
	3: 'Big',
	0: 'None'
};

gform.addFilter('gform_calculation_result', function(result, formulaField) {
	if (formulaField.field_id != calc_field_id) {
		return result;
	}

	var value = config_map[result] ? config_map[result] : '';

	var $el = $('#input_GFFORMID_' + target_field_id);
	if (!$el.length) return result;

	$el.val(value).trigger('change');

	return result;
});

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.