I would like to use survey responses in the calculation of values that then are presented in the survey in real time. I cannot figure out how to show these values in the survey. Should I create the new value as embedded data (in the survey flow) and then add this as piped text or should I use javascript to create a new value that is then set as embedded with (Qualtrics.SurveyEngine.setEmbeddedData("Total",sum_total) in the javascript? I have tried both options and cannot get the calculated values to show up in the survey. These are the calculations I'd like to make:
Take all the family members who are entered with age into a matrix table (drop down choice for age) and summarize them into three categories: kids, adults, seniors. The objective is for these three values to be provided in the survey so the respondent can confirm the answers. (See screen shot below).
!
Page 1 / 1
If you are going to calculate values and present them, you should put them into a descriptive text question instead of an actual question. You can use rich text formatting to achieve the matrix look by putting the text and the calculated values into a table.
Create embedded data (in survey flow) elements for children, adults, senior, and total. Set the values from the question, and put the embedded data into the display table.
Create embedded data (in survey flow) elements for children, adults, senior, and total. Set the values from the question, and put the embedded data into the display table.
WaterSampler: Thanks for your response. I will use rich text formatting to achieve the look I want (instead of using an actual question). I still am uncertain of how to link these javascript calculations to embedded text. I don't think I can make these calculations in the survey flow, but correct me if I am wrong. This is what my javascipt looks like:
Qualtrics.SurveyEngine.addOnReady(function()
{
// These lines read in the entered data on age
let age1="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/1}";
let age2="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/2}";
let age3="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/3}";
// create dummy variables for all the adults
let adult1;
if (age1 > 17) {
adult1 = 1;
} else {
adult1 = 0;
}
let adult2;
if (age2 > 17) {
adult2 = 1;
} else {
adult2 = 0;
}
let adult3;
if (age3 > 17) {
adult3 = 1;
} else {
adult3 = 0;
}
//total all the adults
let adults=adult1+adult2+adult3;
Qualtrics.SurveyEngine.setEmbeddedData("adults",adults_tot);
});
After I include this script I am unable to access it as embedded text and then have it show up in the survey. Do you have any suggestions? Thank you.
Qualtrics.SurveyEngine.addOnReady(function()
{
// These lines read in the entered data on age
let age1="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/1}";
let age2="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/2}";
let age3="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/3}";
// create dummy variables for all the adults
let adult1;
if (age1 > 17) {
adult1 = 1;
} else {
adult1 = 0;
}
let adult2;
if (age2 > 17) {
adult2 = 1;
} else {
adult2 = 0;
}
let adult3;
if (age3 > 17) {
adult3 = 1;
} else {
adult3 = 0;
}
//total all the adults
let adults=adult1+adult2+adult3;
Qualtrics.SurveyEngine.setEmbeddedData("adults",adults_tot);
});
After I include this script I am unable to access it as embedded text and then have it show up in the survey. Do you have any suggestions? Thank you.
A few things,
First: create the embedded data variables before you use them. Do this in survey flow. Using your example, "adults" is the embedded variable name.
Second: you changed the name of your variables in the JS. You're trying to set the embedded value "adults" to a JS variable "adults_tot" which has not been declared or set yet.
Lastly: you can use alert for debugging your code, e.g.,
alert('found '+adults+' adults');
You can make your code easier to follow by reducing the number of variables:
let adults = 0;
let kids = 0;
let seniors = 0;
if (age1 > 17 && age1 < 64) { adults++;} else if (age1 <= 17) { kids++;} else {seniors++;}
repeat the code for age2, age3, etc.
First: create the embedded data variables before you use them. Do this in survey flow. Using your example, "adults" is the embedded variable name.
Second: you changed the name of your variables in the JS. You're trying to set the embedded value "adults" to a JS variable "adults_tot" which has not been declared or set yet.
Lastly: you can use alert for debugging your code, e.g.,
alert('found '+adults+' adults');
You can make your code easier to follow by reducing the number of variables:
let adults = 0;
let kids = 0;
let seniors = 0;
if (age1 > 17 && age1 < 64) { adults++;} else if (age1 <= 17) { kids++;} else {seniors++;}
repeat the code for age2, age3, etc.
Thanks again! This is great and clarifies a few things, but I still can't get the embedded data to show up in the survey in real time. More specifically, I am hoping that the values are calculated once the respondent answers the age question (from a drill down list, is this a problem?)
To clarify, I do have the embedded values in the survey flow:
!
This is how the questions look now:
!
I have the javscript added to Q1 (onReady section) and have the piped text added to Q3. When I use the preview and select ages no calculations show up in Q3 (not even a 0 value).
My javascript is here. (I am not sure how to use alert).
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
// These lines read in the entered data on age
let age1="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/1}";
let age2="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/2}";
let age3="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/3}";
// create dummy variables
let adults = 0;
let kids = 0;
let seniors = 0;
if (age1 > 17 && age1 < 64) { adults++;} else if (age1 <= 17) { kids++;} else {seniors++;}
if (age2 > 17 && age2 < 64) { adults++;} else if (age2 <= 17) { kids++;} else {seniors++;}
if (age3> 17 && age3< 64) { adults++;} else if (age3 <= 17) { kids++;} else {seniors++;}
Qualtrics.SurveyEngine.setEmbeddedData("adults",adults);
Qualtrics.SurveyEngine.setEmbeddedData("kids",kids);
Qualtrics.SurveyEngine.setEmbeddedData("seniors",seniors);
});
Is there an issue with using drill down values? Do I need to add this to a new block or page? I'm at a loss as to why this is not working. Thanks for any advise.
To clarify, I do have the embedded values in the survey flow:
!
This is how the questions look now:
!
I have the javscript added to Q1 (onReady section) and have the piped text added to Q3. When I use the preview and select ages no calculations show up in Q3 (not even a 0 value).
My javascript is here. (I am not sure how to use alert).
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
// These lines read in the entered data on age
let age1="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/1}";
let age2="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/2}";
let age3="${q://QID1%231/ChoiceGroup/SelectedChoicesForAnswer/3}";
// create dummy variables
let adults = 0;
let kids = 0;
let seniors = 0;
if (age1 > 17 && age1 < 64) { adults++;} else if (age1 <= 17) { kids++;} else {seniors++;}
if (age2 > 17 && age2 < 64) { adults++;} else if (age2 <= 17) { kids++;} else {seniors++;}
if (age3> 17 && age3< 64) { adults++;} else if (age3 <= 17) { kids++;} else {seniors++;}
Qualtrics.SurveyEngine.setEmbeddedData("adults",adults);
Qualtrics.SurveyEngine.setEmbeddedData("kids",kids);
Qualtrics.SurveyEngine.setEmbeddedData("seniors",seniors);
});
Is there an issue with using drill down values? Do I need to add this to a new block or page? I'm at a loss as to why this is not working. Thanks for any advise.
To use alert, insert a statement where you want to debug or see the values.
e.g., before creating dummy variables you might say:
alert("age1 recorded as"+age1);
When you preview the survey, you will get a popup showing you what value age1 was set to.
You might have to move the JS to Q3 addOnload. That way Q1 will have all data entered. As you suggested, Q3 might have to be on a new page to insure that all the data from Q1 is in the system.
e.g., before creating dummy variables you might say:
alert("age1 recorded as"+age1);
When you preview the survey, you will get a popup showing you what value age1 was set to.
You might have to move the JS to Q3 addOnload. That way Q1 will have all data entered. As you suggested, Q3 might have to be on a new page to insure that all the data from Q1 is in the system.
Thanks for all the input. Adding a block break solved all the issues. I had hoped to have this show up on the same page, but this will work as well.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.