JavaScript to Autopopulate a response issues | XM Community
Skip to main content
I feel as though I have been able to do this in the past, but doesn't appear to be working now. Was hoping somebody could troubleshoot with a solution.



We often ask multiple choice questions that then drill down into further questions that have carried forward choices from that initial question. For instance, Q1 would ask to select all product users in the home and then Q2 would ask who is the primary product user in the home (carrying forward only selected choices from Q1).



In the case where only 1 choice is made at Q1, I would like to automatically select that response at Q2 and hide it so the respondent doesn't see it.



Here is a snippit of the Javascript I am using to try to accomplish this that isn't working. Essentially saying that if only one choice was selected at the previous question (QID27 in this case) and that choice was X, then select X at this question and skip.





Any thoughts would be appreciated!



Qualtrics.SurveyEngine.addOnload(function()

{

if ("${q://QID27/SelectedChoicesCount}" == 1 && "${q://QID27/SelectedChoicesRecode}" == 1 ) {

this.setChoiceValueByRecodeValue(1, true);

this.getQuestionContainer().hide();

this.clickNextButton();

}



else if ("${q://QID27/SelectedChoicesCount}" == 1 && "${q://QID27/SelectedChoicesRecode}" == 2 ) {

this.setChoiceValueByRecodeValue(2, true);

this.getQuestionContainer().hide();

this.clickNextButton();

}



else if ("${q://QID27/SelectedChoicesCount}" == 1 && "${q://QID27/SelectedChoicesRecode}" == 3 ) {

this.setChoiceValueByRecodeValue(3, true);

this.getQuestionContainer().hide();

this.clickNextButton();

});
It looks like you are missing a } after the last if. Also, use addOnReady instead of addOnload.



I recommend using the following script instead. It doesn't rely on specific question ids or recodes.

https://gist.github.com/marketinview/8310ba82acde38be62f007095bbe0278



Hiding the question doesn't help much because the respondent will still see the question flash by. Instead add an empty div to the top of the question with enough height to push the text off the bottom of the screen, then hide it if it doesn't automatically advance.
Tom, thanks once again! You are amazing. This is a much more simple solution, and more foolproof.



One more question on your 2nd comment. I do see that hiding doesn't do any good because it still flashes. Not being fluent in coding, do you have an example of adding the empty div and what that would look like + where exactly to input it?
> @GeoffK said:

> Tom, thanks once again! You are amazing. This is a much more simple solution, and more foolproof.

>

> One more question on your 2nd comment. I do see that hiding doesn't do any good because it still flashes. Not being fluent in coding, do you have an example of adding the empty div and what that would look like + where exactly to input it?



HTML:

```

<div id="padTop" style="padding-top:2000px"></div>

Your question text

```

JS (if JS doesn't click next):

```

jQuery("#padTop").hide();

```
Perfect, thanks!

Leave a Reply