Embed the answer that corresponds another answer | XM Community

Embed the answer that corresponds another answer

  • 7 March 2019
  • 4 replies
  • 32 views

Badge +1
Hello,

I am trying to record the answer depending on another answer. What I mean is that, let's say that I have 3 blocks. Each block has the same questions, like:

Block #1: First trip
Q1. How many miles did you travel? (text entry, only number)
Q2. What was the trip purpose? (multiple choice)

Block #2: Second trip
Q3. How many miles did you travel?
Q4. What was the trip purpose?

Block #3: Third trip
Q5. How many miles did you travel?
Q6. What was the trip purpose?


Then, I want to record the trip purpose if the trip mile is the longest among the three.
So, my logic is like,

var max = Math.max("${q://QID1/ChoiceTextEntryValue}", "${q://QID3/ChoiceTextEntryValue}", "${q://QID5/ChoiceTextEntryValue}");
Qualtrics.SurveyEngine.setEmbeddedData("max", max);

//From here, I just say my logic. It will not make sense in terms of javascript
if "Q1" is max, record the answer of Q2;
else if "Q3" is max, record the answer of Q4;
else record the answer of Q6;
//

Is it possible in Qualtrics?

Thank you very much in advance.

4 replies

Hello @jhwang ,

Since we cannot concatenate the piped text and get the value the in the JS, this approach is not possible, but yes you can maintain an array of all second question of each block and based on the max value pick the respective array element.
Badge +1
Thanks, @Shashi.

I think I got a hint from you.

Here is what I did:

Qualtrics.SurveyEngine.addOnReady(function()
{

var i1="${q://QID1/ChoiceTextEntryValue}";
var i2="${q://QID2/ChoiceTextEntryValue}";
var i3="${q://QID3/ChoiceTextEntryValue}";
var i4="${q://QID4/ChoiceGroup/SelectedChoices}";
var i5="${q://QID5/ChoiceGroup/SelectedChoices}";
var i6="${q://QID6/ChoiceGroup/SelectedChoices}";

var max = Math.max("${q://QID1/ChoiceTextEntryValue}", "${q://QID2/ChoiceTextEntryValue}", "${q://QID3/ChoiceTextEntryValue}");
Qualtrics.SurveyEngine.setEmbeddedData("max", max);

var purpose;
if(max = i1){
purpose = i4;
}
else if(max = i2){
purpose = i5;
}
else{
purpose = i6;
}
Qualtrics.SurveyEngine.setEmbeddedData("purpose", purpose);

});


I think it works. If you find any errors, please let me know.
> @jhwang said:
> Thanks, @Shashi.
>
> I think I got a hint from you.

> I think it works. If you find any errors, please let me know.
That's correct just make sure you have` if(max==i1) ` while deploying
Badge +1
@Shashi
Thank you very much!

Leave a Reply