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

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

  • 17 September 2020
  • 11 replies
  • 254 views

Userlevel 3
Badge

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.

icon

Best answer by TomG 18 September 2020, 23:00

View original

11 replies

Userlevel 7
Badge +27

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();
}

Userlevel 3
Badge

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

Userlevel 3
Badge

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

Userlevel 7
Badge +27

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");

Userlevel 3
Badge

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

Userlevel 3
Badge

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


Userlevel 3
Badge

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"?

Userlevel 3
Badge

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();
}
});

Userlevel 7
Badge +27

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).

Userlevel 3
Badge

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?

Userlevel 7
Badge +27

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

Leave a Reply