How to assign a new value to {q://QID98/ChoiceGroup/SelectedChoices} | XM Community
Skip to main content
Solved

How to assign a new value to {q://QID98/ChoiceGroup/SelectedChoices}

  • September 17, 2020
  • 11 replies
  • 457 views

Forum|alt.badge.img

In my survey, there is a multiple-choice Question 98, which has answers: "Yes" or "No".
After the survey user typed in the answer and variable {q://QID98/ChoiceGroup/SelectedChoices}  has been assigned  "Yes" or "No", I want  to conditionally modify its value based on  a previous question, say, Question 50.
Below is a pseudocode to show what I want to achieve:
If  "{q://QID50/ChoiceGroup/SelectedChoices}" == "Yes" {
{q://QID98/ChoiceGroup/SelectedChoices}="Yes" }
 
In other words, If Question 50  was answered "Yes", then the value of the Question 98 answer is forced to be 'Yes' (regardless of what the survey user actually typed).
If Question 50  was answered "No", then the value of the Question 98 answer will be what the survey user actually typed.
 
Can I  use setChoiceAnswerValue and what will be the code to assign a new value to {q://QID98/ChoiceGroup/SelectedChoices} ?
 
I understand that what I am trying to achieve  may not be the best practice , but a have a long survey  and the answer to Question 98 is used in many subsequent questions and flow logic. So for me, it's much easier to modify just one Question 98 and make its answer depend on Question 50 instead of rewriting the entire survey with numerous conditional logic based on Question 98.

Best answer by TomG

Yes, it will work as long as Yes is always last.

11 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 17, 2020

You can write JavaScript to automatically answer 'Yes' to Question 98 if they answered 'Yes' to Question 50 then click the Next button. Something like:
if("{q://QID50/ChoiceGroup/SelectedChoices}" == "Yes") {
jQuery("#"+this.questionId+" input[type=radio]:first").prop("checked",true);
this.clickNextButton();
}


Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 17, 2020

Actually, the Question 98 is multiple-choice one with three answers: "No", "Yes", and "not sure" and Dropdown List. See the screenshot:
Capture.PNG


Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 17, 2020

Capture1.png
How will jQuery( ) code look like?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 17, 2020

https://www.qualtrics.com/community/discussion/comment/30363#Comment_30363Assuming the choice id of Yes is 2, change the second line to:
jQuery("#"+this.questionId+" select").val("2");


Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 18, 2020

I placed the code to Qualtrics.SurveyEngine.addOnload().
Correct?


Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 18, 2020

Is the JS code compatible with Question 98 Validation Option "Force Response"?



Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 18, 2020

I removed the condition, but the code below is not working:
neither
Qualtrics.SurveyEngine.addOnload(function()
{
   jQuery("#"+this.questionId+" select").val("2");
   this.clickNextButton();
});
nor
Qualtrics.SurveyEngine.addOnReady(function()
{
   jQuery("#"+this.questionId+" select").val("2");
   this.clickNextButton();
});
nor
Qualtrics.SurveyEngine.addOnUnload(function()
{
  jQuery("#"+this.questionId+" select").val("2");
   this.clickNextButton();
});

When checked in subsequent questions, ${q://QID98/ChoiceGroup/SelectedChoices} does not have any value. And display logic and validation logic do not recognize Question 98 as selected "Yes" .
So. probably, jQuery("#"+this.questionId+" select").val("2") is not working for me.
Only  this.clickNextButton() does work :-)
If I remove this.clickNextButton() and keep jQuery("#"+this.questionId+" select").val("2"), should the page load with Question 98 selected as "Yes"?


Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 18, 2020

Finally, I had to change question type to "Single Answer" and the below code worked for me:
Qualtrics.SurveyEngine.addOnload(function()
{
  var p = "${q://QID50/ChoiceGroup/SelectedChoices}";
if( p == "Yes") {
   jQuery("#"+this.questionId+" input[type=radio]:last").prop("checked",true);
   this.clickNextButton();
}
});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 18, 2020

https://www.qualtrics.com/community/discussion/comment/30378#Comment_30378That doesn't match the order of the choices in the drop down you posted. If your choices are randomized, this approach won't work (it will only be correct about 1/3 of the time).


Forum|alt.badge.img
  • Author
  • Level 1 ●
  • September 18, 2020

I changed the order as follows:
order.PNGand my question type is
type.PNGI have never intentionally randomized my choices (and do not know what it is).
Should my setup work correctly now?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • September 18, 2020

Yes, it will work as long as Yes is always last.