JavaScript for creating grouped variable | XM Community
Skip to main content

I'd like to create an embedded data field

age3 
which groups an age variable (
answered_age
, type Number), but I'm unable to get the field to populate when testing the survey.
age3
is initiated as a Text Set variable at the top of the survey flow. I've included the code I've written below.
Qualtrics.SurveyEngine.addOnload(function() {
var age3 = "55+";
if (answered_age > 18 & answered_age <= 34) {age3 = "18-34"};
if (answered_age > 35 & answered_age <= 54) {age3 = "35-54"};
Qualtrics.SurveyEngine.setEmbeddedData("age3", age3);
)}
Can anyone point me in the right direction? I'd appreciate any help on this! Thank you so much!

Is there a reason it needs to be assigned via JavaScript? versus using either:
a. assignment of the grouping variable via the survey flow, or
b. creation of a custom variable (available only after data is collected)
We use Survey Flow to create new variables when we know up front it will be a need [e.g. branching based on responses, leading to setting Embedded data values, making sure in the Options on the Embedded data set to make the type "Text Set" in order to make it usable as a variable in analysis] -- or via custom bucketing variables [on the Data & Analysis tab] when we don't find out until after data collection, that responses needs to be regrouped.
I can 100% give guidance on how to do it in the Survey Flow, if that will help you -- but not via JavaScript (we try to rely on base functions vs JS whenever possible, which is mostly because none of our team is a programmer, and partly because sometimes something changes behind the scenes in either Qualtrics or browser defaults can change how JS operates).
If you need JS help there are a lot of other members of the community who are fantastic at that and who can probably help (if it's actually required that you accomplish it via JS, for some reason I haven't grasped). But if you need help in how to do it in Survey Flow, just LMK whether you're giving them a dropdown of ages or a text entry with "number" validation, and I can provide an example of how to set that.


Thanks CarolK , but I would prefer to do this with Javascript. There are a few variables I'd like to bucket/recode, and doing all of this in the survey flow, while possible, will be fairly clunky. I'm making these changes to our template survey which we use to create all surveys from, and custom variables created via bucketing or other built-in functions are not copied over when a new survey is created.


If I understand correctly, answered_age is another prefilled ED. If so here's the way :
var answered_age = "${e://Field/answered_age}";
var age3 = "55+";
if (answered_age > 18 & answered_age <= 34) {age3 = "18-34"};
if (answered_age > 35 & answered_age <= 54) {age3 = "35-54"};
Qualtrics.SurveyEngine.setEmbeddedData("age3", age3);


Leave a Reply