Javascript Basics | XM Community
Solved

Javascript Basics

  • 12 April 2018
  • 5 replies
  • 77 views

Badge +1
Hello
Can anyone help me understand how to use JavaScript within Qualtrics? I'm trying to say "If QID1=2 then set QID2=2", but I can't work out how to write the "QID1=2" part. This is what I have so far, but how do I refer to QID1 from within QID2? They are both Multiple Choice.

[Within QID2]
Qualtrics.SurveyEngine.addOnload(function()
{
if ( ??? getChoiceValue(2) == true)
{this.setChoiceValue(2, true)}
});

I've been looking at the API documentation, but there are very few examples, and none on how to use it to refer to another question.

Thanks,

Tricia
icon

Best answer by TomG 12 April 2018, 18:35

View original

5 replies

Userlevel 7
Badge +38
@TriciaB i don't use Javascript that much but when I have I've often been able to use piped text to get the variables needed. I think you should be able to replace your line with ??? in with :

if ("${q://QID1/ChoiceGroup/SelectedChoices}"==2)

Use the piped text feature to make sure that the selected choice for your QID1 is the same.
Userlevel 7
Badge +27
@TriciaB,

Are QID1 and QID2 on the same page? If so, do they have to be?
Badge +1
Thanks for your replies.

This syntax below doesn't work. Is that what you meant, @bstrahin? (I've corrected the ID number in my actual syntax).
if ("${q://QID1/ChoiceGroup/SelectedChoices}"==2)
{this.setChoiceValue(2, true)}

QID1 and QID2 are not on the same page, no. My intention is to hide QID2 and use it for filtering and validation later on, but it's not currently hidden.
Userlevel 7
Badge +20
@TriciaB
If QID1 is not on the same page with QID2 then you can bring the value of QID1 on QID2 page.
!

Just place the above tag within QID2 question text and then use jQuery("#QID1Value").text(); which will give you the response from QID1 and then you would be able to write the "if condition" on it to set the value for QID2.

Hope you are familiar with JQuery
Userlevel 7
Badge +27
@TriciaB,

@bstrahin is close, but I'm guessing that "2" is the recode, not the label. So, I think what you really want is:
```
if(parseInt("${q://QID1/SelectedChoicesRecode}") == 2) this.setChoiceValue(2, true);
```

Leave a Reply