Help explaining javascript needed for Auto filling an answer | XM Community
Skip to main content

Help explaining javascript needed for Auto filling an answer

  • February 8, 2022
  • 3 replies
  • 308 views

Forum|alt.badge.img+2

Hello-
I am trying to create an example survey flow for my team with the javascript needed to autofill an answer from a previous question in the following scenarios. I have come across examples of javascript for these scenarios, but I am completely new to javascript and finding it hard to decipher what parts I would adjust for my situation. Would love if someone would be open to writing instructions for each in a way that a javascript newbie can understand. Thank you for any help you may be able to give.
Scenario 1 (autoselect all the answers in 2nd question)
Q1. Which animal do you like?

  1. Fish

  2. Dogs

  3. Cats

  4. None of the above

::PAGE BREAK::
Q2. Which animal do you dislike?
(PIPE IN ANSWERS FROM ABOVE)
  1. Fish

  2. Dogs

  3. Cats

  4. None of the above

::PAGE BREAK::
Q3. Next question here
HELP NEEDED: What javascript would I add to Q2 to autoselect 1-3 if in Q1 the person selected 4 (none of the above) and would move to the next page
Scenario 2 (autoselect 1 answer in 2nd question)
Q1. Which animal do you like?
  1. Fish

  2. Dogs

  3. Cats

::PAGE BREAK::
Q2. Which animal do you like most?
(PIPE IN SELECTED ANSWERS FROM ABOVE)
  1. Fish

  2. Dogs

  3. Cats

::PAGE BREAK::
Q3. Next question here
HELP NEEDED: What javascript would I add to Q2 to autoselect the answer they selected in Q1 if they only selected 1 animal and would move to the next page
Again, I really appreciate as much detail for a newbie you could provide and THANK YOU for any one that does.

3 replies

vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 18, 2022

Aship There is most probably more elegant ways to do it, but this should work. Inspect your pages to retrieve the right questions and choices IDs.
1:
Qualtrics.SurveyEngine.addOnReady(function()
{
if("${q://QID2/ChoiceGroup/SelectedChoices}"=="None of the above") {
jQuery("#"+this.questionId+" input[choiceid='x1']").prop("checked",true);
jQuery("#"+this.questionId+" input[choiceid='x2']").prop("checked",true);
jQuery("#"+this.questionId+" input[choiceid='x3']").prop("checked",true);
this.clickNextButton();
}

});
2:
Qualtrics.SurveyEngine.addOnReady(function()
{
if("${q://QID5/SelectedChoicesCount}"=="1") {
if("${q://QID5/ChoiceGroup/SelectedChoices}"=="Fish") {
jQuery("#"+this.questionId+" input[choiceid='x1']").prop("checked",true);}
if("${q://QID5/ChoiceGroup/SelectedChoices}"=="Dogs") {
jQuery("#"+this.questionId+" input[choiceid='x2']").prop("checked",true);}
if("${q://QID5/ChoiceGroup/SelectedChoices}"=="Cats") {
jQuery("#"+this.questionId+" input[choiceid='x3']").prop("checked",true);}

this.clickNextButton();
}
});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • February 18, 2022

More flexible approaches.
Scenario 1, select all choices but last:
Qualtrics.SurveyEngine.addOnReady(function() {
if("${q://QID2/ChoiceGroup/SelectedChoices}"=="None of the above") {
jQuery("#"+this.questionId+" input[choiceid]:not(:last)").prop("checked",true);
this.clickNextButton();
}
});
Scenario 2, if one choice displayed select it:
See this.



Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • March 2, 2022

vgayraud & TomG THANK YOU so much! I am sorry this took so long for me to say. I am new to this community and did not realize how to check back to know someone left a comment. You all are the best!