In my survey, I currently have a question, set up as a constant sum matrix. In this original matrix, the Statements are various procedures while the Scale Points are the surgeries per month. From that, I was trying to pipe the top 3 reported procedures, based on the reported numbers, throughout the rest of the survey as statements in various matrix questions. Is there a way to do this, or would anyone know what the JS would look like for this?
This function should be able to do that for you:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
const qid = this.questionId;
const tot_choices = Qualtrics.SurveyEngine.registrynqid].getChoices().length;
choice_set ={};
for(i = 1; i<=tot_choices;i++){
option_score = document.getElementsByName("QR~"+qid+"~"+i)u0].value;
option_name = document.getElementById("header~"+qid+"~"+i).innerText.trim();
choice_setqoption_name] = option_score;
}
const sorted_choices = Object.fromEntries(
Object.entries(choice_set).sort((e,a],,b]) => b-a)
);
i = 1;
for(keys in sorted_choices){
Qualtrics.SurveyEngine.setEmbeddedData("top_"+i, keys);
i++;
}
});
A few things to note:
top_1
refers to the top choices,top_2
to the next etc. Each of these are embedded data fields and can be used like any other. Though, they may not show up in your GUI editor- You will need to have a page break between the choice selection and using them. Embedded Data setting doesn't work on the same page.
- If two choices have the same value, then one that appears first in your order will get precedence. Eg: If
Option 3
andOption 4
both equal to 20, thenOption 3
will be first andOption 4
after that.
Thank you! Should this be added to the question in which the survey numbers are being reported or should this JS be in the questions dependent on the ranking question?
Add it to the question where you are asking them assign numbers.
Ok I did that. And I made the statements in the following questions embedded data as "top_1," "top_2," etc. When previewing the survey the statements are not showing up/are blank.
I have never coded before/used this survey site. Do I seem to be doing something wrong or can you walk me through this step-by-step? I really appreciate your help.
Embedded data requires one page to update. So, if you are trying to use them on the same page, it won't work. When you refer to the embedded data in the question text, it looks like this
${e://Field/top_1}
I have the question with the JS you provided attached to it. Then there is a page break. Then this is how question contingent on the JS appears. However when the survey is taken, the statements are blank. I have attached a picture to give more color.
Please share the screen of the first question, where you are asking people to make a choice. Also, what type of a question is it? How many columns do you have?
This is the first question, which is set up as a constant sum matrix.
Then the second question, relying on what is reported above, should have 3 statements in a constant sum matrix table. However the statements are reliant on what is reported in the above question.
You are carrying choices forward from another question.
Which is causing the function to fail.
See if this works:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
const qid = this.questionId;
available_choices = Qualtrics.SurveyEngine.QuestionInfo.qid].Choices;
tot_choices = ]
for(keys in available_choices){
tot_choices.push(keys);}
choice_set ={};
i=1;
tot_choices.forEach(item =>{
option_score = document.getElementsByName("QR~"+qid+"~"+item)0].value;
option_name = document.getElementById("header~"+qid+"~"+i).innerText.trim();
choice_set.option_name] = option_score;
i++;
});
const sorted_choices = Object.fromEntries(
Object.entries(choice_set).sort((b,a],r,b]) => b-a)
);
i = 1;
for(keys in sorted_choices){
Qualtrics.SurveyEngine.setEmbeddedData("top_"+i, keys);
i++;
}
});
The code is in the JS, the issue seems to be the statement text field. It continues to appear blank even when the code is working properly according to the console.
Probably because there's something about the questions set up that you haven't mentioned. eg: I got to know from the screen shot that you're carrying forward choices. Sharing the QSF would help, but if it's confidential, you can get in touch with Qualtrics support.
https://community.qualtrics.com/XMcommunity/discussion/comment/32533#Comment_32533Hey, i have the same problem. I have no carrying forward choices. I have a constant sum question with 17 pictures. And after that i will have a question with the top 3. But with the code it continues to appear bland. Can you help me? Thank you in advance
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.