This article shows how to add two strings together using the script editor and the logic block of the output field. Using wither of these methods will produce the same result, so give each a try and see which type of scripting you prefer!
Logic Block
The following script used in the Logic tab for the field ResultWord will take the values of two form fields (which are not in a repeat), 'Word1' and 'Word2', join them together with a space in between, and set this character string as the value for the ResultWord field:
Under 'Trigger' select 'Javascript' and add the following code in the box:
result = true;
Under 'Action', select 'Value' and add the following code in the box:
value = data.Word1 + ' ' + data.Word2;
Save the Action and the Logic, and then the ResultWord field and the logic will run!
Script Editor Function
This function takes two Form Field values (which are not in a repeat), ‘Word1’ and ‘Word2’, adds them together with a space in between (i.e. ' '), and then places the answer into the ‘ResultWord’ Form Field:
function addWord(){ var val1 = Form.data.Word1; var val2 = Form.data.Word1; var total = val1 + ' ' + val2;
if ($('.formio-component-ResultWord')) {$(form).setVal('ResultWord', total)}
Form.data.ResultWord = total; };
This function would be placed inside the Script Editor. To trigger the function when each of the two input fields change, the following two lines of code are required:
$('.formio-component-Word1').find('input').change(function() { addWord() })
$('.formio-component-Word2').find('input').change(function() { addWord() })
Comments
0 comments
Article is closed for comments.