Set answers for hidden question based on previous question's answer | XM Community
Question

Set answers for hidden question based on previous question's answer

  • 19 April 2019
  • 3 replies
  • 314 views

Userlevel 1
Badge +1
A newbie to Qualtrics custom code with a request for advice ... even better if someone has a code snippet that is close to what I want ...

I have a survey with several matrix single answer likert questions. Depending on a response to a question prior to the matrix question I would like to set the selected answers to the matrix question, ideally without having the survey participant see this being done or need to press the next button. The previous question is a "yes / no" question, so it's recode value for the answer isn't what I'm trying to use to set the matrix question answers.

As far as I can tell the only way to do this in Qualtrics is to use JavaScript in the matrix questions. Question #1: Am I correct, or is there another way to get responses recorded for a question the survey participant does not see?

I'd like to re-use the JavaScript code code for multiple matrix questions, so hard coding the choiceID, recodeValue, subid, etc. of the matrix questions isn't a good idea.

Studying the Qualtrics documentation and posts I think this is the way to go:

Use Qualtrics.SurveyEngine.QuestionInfo to iterate through the matrix question. This will give me the IDs and subids I need to set.

Use piping syntax to get the previous questions answer, set it to a JS variable, used in conditional coding.

Use the previous answer to decide how to answer each row of the matrix question. Use setChoiceValue to set the choice. I have experimented with setChoiceValueByRecodeValue. Question #2: I couldn't find too many examples that illustrate the use cases for the Qualtrics JavaScript APIs. I assume setChoiceValue uses a string that matches the value I want to set, versus using the recode value. Is that right?

Use clickNextButton to cleck the next button.

Question #3: Any comments on teh above approach?

Question #4: How do I make this "invisible" to the survey participant? I saw a post onn the forum where someone mentioned they "hide the skin" in the JavaScript code, but I couldn't find anything that helped me understand what that means.

If you've read this far many, many, many thanks!

3 replies

Badge +1
You are on the right track.

To hide a question put this into your javascript addOnLoad for the hidden question:
this.getQuestionContainer().hide();

Setting the matrix table uses row, column indexing followed by the value:
this.setChoiceValue(1,1,-1);
this.setChoiceValue(2,1,-1);
The above example sets rows 1 and 2, column 1 to a value of -1.

Consider using embedded data. You can set an ED variable when a question is submitted, then use the ED in your hidden question. After using the ED, reset it to a null value to reuse for subsequent questions.
Userlevel 1
Badge +1
@WaterSampler - thanks for the advice. I've experimented and now have a few more clarifications and questions.

The behavior I'm now after is this:

If the survey participant answered a previous questions with "None of the above" (recode value 5) then set the current matrix question's answers per row to recode value of 1. The participant will be instructed to press next to accept the matrix's question answers as set by the JS code. The participant can alter the matrix question answers if desired and press next.

While on the matrix question the participant should be allowed to hit previous and change the answer to the previous question from "None of the above" to another value. When they advance back to the matrix question any previously set answers are removed and the question is reset so they have to answer the question again.

My first problem is that by eliminating the hiding of the next button and allowing the participant to alter the answers I can't get passed the forced validation. So for example if the participant changes the matrix question's answers from the first recode value to another when they press the next button the error "Please answer this question" is displayed. The only way to get passed that is to answer the question with the recode value set by the JS code.

Here's my code:

Qualtrics.SurveyEngine.addOnload(function()
{
// this.getQuestionContainer().hide();
var skillsValue = "${q://QID6/SelectedChoicesRecode}";
// Qualtrics.SurveyEngine.setEmbeddedData( 'skillsSource', skillsValue);
if (skillsValue == "5") {
this.setChoiceValueByRecodeValue("1", "1", "1");
this.setChoiceValueByRecodeValue("2", "1", "1");
this.setChoiceValueByRecodeValue("3", "1", "1");
}

});

Qualtrics.SurveyEngine.addOnReady(function()
{

//var skillsValue = "${q://QID6/SelectedChoicesRecode}";
//if (skillsValue == "5") {
// this.hideNextButton();
// var that = this;
// (function(){that.clickNextButton();}).delay(1);
//}

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});
Badge +1
When you say forced validation, do you mean that you have forced response and validation enabled? Those are two distinct options questions. If so, does your validation look for certain responses? The message implies that you have forced response set and there is nothing entered.

You can send me the preview link as a message so that I can see what you are experiencing. Go to preview survey, copy the link from the browser and message the link.

Leave a Reply