Solved
Help with JavaScript and piped text
I am creating a survey where participants are asked a question, for example:
"Do you believe this? (Check all that apply)" [this is a multiple-answers multiple choice question]
Option A
Option B
Option C
Option D
Option E
Then they are presented with a screen. This is all text (no questions). Essentially, the screen will say:
"Blah blah...Look at the options below. The first two options (font color is green)....and the last three options (font color is red)....The ones that are highlighted in yellow are the ones you selected. [I want the options to be listed in this order with the font colors, and highlighted based on their selection.]
Option A
Option B
Option C
Option D
Option E
I know there are ways to have the options they selected appear on a following screen using embedded data and piped text (e.g. https://www.qualtrics.com/community/discussion/2224/how-to-display-unselected-choices-from-one-question-in-a-list-view-using-descriptive-text-question), but I was wondering if you can just highlight the text based on the options they select in the previous multiple-answers multiple choice question.
I am new to java script and this coding stuff, so it would be great if anyone could help out.
Thank you sooo much!
Best answer by Anonymous
Hello @jess5 ,
Paste the below code in the next question("Look at the below options.....") js(OnReady) and replace the QID in the code with the previous question QID on line `var ac=` and `var sc=`
var ac= "${q://QID9/ChoiceGroup/AllChoices}";
var ac_array=ac.split(", ")
var sc="${q://QID9/ChoiceGroup/SelectedChoices}";
var sc_array=sc.split(", ");
console.log(sc_array);
var element="<span><ul style='color:red' id='customUl'>";
var i;
for( i=0;i<ac_array.length;i++){
if(sc_array.includes(ac_array[i])){
console.log(ac_array[i]);
element=element+"<li style='background:yellow'>"+ac_array[i]+"</li>";
}else{
element=element+"<li>"+ac_array[i]+"</li>";
}
}
jQuery(".QuestionText").append(element+"</ul></span>");
jQuery("#customUl li:eq(0)").css("color","green");
jQuery("#customUl li:eq(1)").css("color","green");
Output:
!

Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.


This is what I am imagining...
Thank you!