Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Use of merged fields as "input suggestions" in numeric field

Dear all,


I have a question regarding the use of merged fields in numeric input fields. I guess this was asked and answered before, but I was not able to find a post in this forum about this.

This is my situtation:
The particpants of my study have been recruited. During the recruitment they already answered some questions. I would like to verify their answers from the recruitment at the beginning of the study and give them the chance to correct the answers if needed.

As an example let´s say they where asked how many units of specific products they bought in the last year.
For respondent R1 I now have the information, that he bought 100 units of A and 50 units of B. I save this information as merged fields (field name: "NoUnitsA"/ "NoUnitsB").
At the beginning of the survey I ask a select question: "During the recruitment you stated that you bought [% NoUnits A%] units of product A and [% NoUnits B%] units of product B. Is that correct?"  
Answer possibilities are: "Yes" or "No".
If R1 answers "No", he is then asked in the next question: "How many units of the following products did you buy last year?"
Here I use a grid with numeric input fields:
Product A: ____
Product B: ____

What I would like to do is adding the values [% NoUnitsA %] and [% NoUnitsB %] as the default input in the numeric input fields. The respondent then can change the values which are wrong but can also leave single values which are correct as they are without having to make an input.

Can anybody help me with that issue?


Best regards,

Jonathan
asked Aug 6, 2018 by JoGu (250 points)

1 Answer

0 votes
Try adding this to your grid question:

<script>
$(document).ready(function(){
    var mergedFieldAnswers = {
        'GridQ_r1_c1': [% merged1 %],
        'GridQ_r2_c1': [% merged2 %],
        'GridQ_r3_c1': [% merged3 %]
    };
    
    for (var question in mergedFieldAnswers) {
        if (mergedFieldAnswers.hasOwnProperty(question) && !$('#' + question).val().length) {
            $('#' + question).val(mergedFieldAnswers[question]);
        }
    }
})
</script>


Lines 4-6 must be updated with a comma-separated list of pairs for each product: the left part updated with the name of a variable in the grid question, the right part updated with the related merged field variable.
answered Aug 6, 2018 by Zachary Platinum Sawtooth Software, Inc. (114,750 points)
Hi Zachary,

Thank you very much for your help. Unfortunately the code does not work in my case. When the question is opened no values are displayed in the numeric input fields.

I am using a Custom JavaScript Verification  on this page. Does this maybe cause a problem?
Custom JavaScript verification should not have any effect on the page before the respondent attempts to submit the page.  To be clear, though, my code above should not be used as custom JavaScript verification.

Can you please open your browser's console while running this question and see if any errors appear?  You can usually open your browser's console by pressing F12 in the browser and navigating to a tab labelled Console.  Errors are usually displayed as any red text in the console.
I added the code in the footer of the question, so as you explained this should not be an issue.

I also opened the browser console as you described. Indeed there appears an error : I get an "Uncaught SyntaxError: Unexpected token }" for the closing brace after the declaration of the var mergedFieldAnswers (so the brace in line 7 in the code you posted above).
Also the merged field variable for the last row in my grid is not loaded. In the "sources" tab of the browsers console I can see the merged field variables as numbers in all rows but the last one. In the last one there is only empty space.
It sounds like one of your merged fields in Lighthouse Studio is not being set for the respondent password you are running.  Is this intentional?  If so, how do you want this question to behave when a merged field is left empty?
You were right! One merged field was not set. I just corrected that and the code works perfectly.
I did not intend to leave the field empty in this case, but anyway it could happen. In this case the best would be to put a "0" in the input field.
Great.  If you want empty fields to default to zero, the easiest way might be to wrap the Sawtooth Script with JavaScript's Number function, like this:

Number([% merged1 %])
Perfect! Thank you very much.
...