Set hidden question value with JavaScript | XM Community
Solved

Set hidden question value with JavaScript

  • 4 August 2020
  • 6 replies
  • 195 views

Hello everyone,
this is my first post here and I am writing because I am stuck with code I can't figure out how to do.
I am also new to Qualtrics coming from ConfirmIt background and maybe that's the essence of my problem.
To give you a little bit more background let me explain what I am trying to achieve.
We survey a number of respondents for part of who we have some background, preloaded data. The problem is that we only have this for some of them so we need to ask questions about e.g. gender to those for who we don't. Therefore I have two questions:
pl_gender - it stores the preloaded data
gender - it is being asked if pl_gender is null
For analysis purposes I use "gender" question only so I need it to be populated if pl_gender is not null but I can't set the "gender" field in Qualtrics DB by any means.
For those of you who have some experience in ConfirmIt I am basically trying to replicate this code:
//check if there is preloaded gender data
if (f('pl_gender').toBoolean())
{
//set gender answer to whatever was uploaded to pl_gender question
f('gender').set(f('pl_gender'))
}

I know that the workaround for that would be to generate a third question e.g. "GenderCombined" as embedded and use SetEmbeddedData but that is my last resort solution as the number of times I need to do it is purely horrendous. On top of that there is an ETL process to our database and the amount of work it would require to change is very discouraging.
Thanks all for your help!
Greg


icon

Best answer by SurajK 5 August 2020, 17:09

View original

6 replies

Userlevel 5
Badge +4

Hi Greg ,
Use the below code at gender question, it will store the pl_gender data in gender question itself if it is not blank. Do not add any base logic for gender question. The below code will automatically take care if it is not null and not in preview mode.
Qualtrics.SurveyEngine.addOnReady(function()
{
var plgender = "${e://Field/pl_gender}"
if(plgender != '' || plgender != null)
{
this.setChoiceValueByRecodeValue(plgender,true)
if ("${e://Field/Q_CHL}" != "preview"){
jQuery('#Wrapper').css('visibility','hidden');
jQuery('#Wrapper').css('opacity','0');
jQuery('#NextButton').click()
}
}
});

Hi @SurjaK
Thank you very much for your answer. This is a very good attempt and it got me far further then I originally was, but it doesn't solve my problem completely.
The problem I've got is that gender is not the only question on my page so that part of the code kills it :)
jQuery('#Wrapper').css('visibility','hidden');
jQuery('#Wrapper').css('opacity','0');
jQuery('#NextButton').click()
Nevertheless it actually widens my perspective of how Qualtrics is handling stuff. I tried to avoid "this." solution as, like you've pointed out, it requires the question to be physically generated. This was my suspicion and biggest difference from ConfirmIt as the latter can access the project DB from survey level. What I mean by that is that you can edit respondent answers in survey from every place by forcing stored values changes (doesn't require "this." kind of solution).
The other problem I've got is that when I access respondent in progress data and download it as CSV, Qualtrics changes answer codes from e.g. F to 2. I am suspecting that this is why this part of the code doesn't work:
this.setChoiceValueByRecodeValue(plgender,true)
I've put the code on diet and added console logs like this:
var plgender = "${e://Field/pl_gender}"
    console.log(plgender);
    if(plgender != '' || plgender != null)
    {
        console.log("BEFORE This SET");
        this.setChoiceValueByRecodeValue(plgender,true)
        console.log("After This SET");
    }
And all seem to work correctly so in console I get:
F
BEFORE This SET
After This SET
yet the value is not being set. So when I click on the value manually and check the data, rather than F I get 2 in responses in progress.

Userlevel 5
Badge +4

Looks like in your background variable (pl_gender), data is getting passed as M or F. So, in this case you will have to modify the above JS code,
var plgender = "${e://Field/pl_gender}"
    console.log(plgender);
    if(plgender != '' || plgender != null)
    {
        if(plgender == 'M' || plgender == 'm'){this.setChoiceValueByRecodeValue(1,true)}
else if(plgender == 'F' || plgender == 'f'){this.setChoiceValueByRecodeValue(2,true)}
    }

Also, if you have more than one questions on same page (as you mentioned), you can do like, create gender question as prg_gender which will be just programming variable like we used to do in Confirmit and create gender variable as your main question which will be hidden.
Create this gender question after the pagebreak i.e. after prg_gender question and in data you can refer the gender variable.
So, in your gender (hidden question), add the below code, just change the ID value of prg_gender as per your survey. The below code will take care everything and you can refer gender variable for the combined data.
Add the base logic to your prg_gender question to display if pl_gender is empty.
Qualtrics.SurveyEngine.addOnReady(function()
{
var plgender = "${e://Field/pl_gender}"
var prg_gender = "${q://QID13/SelectedChoicesRecode}"
if(plgender != '' || plgender != null)
{
if(plgender == 'M' || plgender == 'm'){this.setChoiceValueByRecodeValue(1,true)}
else if(plgender == 'F' || plgender == 'f'){this.setChoiceValueByRecodeValue(2,true)}
}
else
{
this.setChoiceValueByRecodeValue(prg_gender,true)
}
if ("${e://Field/Q_CHL}" != "preview"){
jQuery('#Wrapper').css('visibility','hidden');
jQuery('#Wrapper').css('opacity','0');
jQuery('#NextButton').click()
}
});

Thank you very much for your answer SurjaK.
One last question I have, and it would make my understanding of the software much bigger, what is the difference between:
var plgender = "${e://Field/pl_gender}"
var prg_gender = "${q://QID13/SelectedChoicesRecode}"
What I mean by that is that the first one refers to e:// and the second one to q://
Thank you!

Userlevel 5
Badge +4

The first one is referring to embedded value, it's the syntax of referring any embedded variable and the second one is the question's selected answers codes.
You can check this using the piped text option in the question text, like try to pipe the embedded variable, try to pipe the question text, choice, recode, and also you can try other methods as well.

image.png

Right, that's all clear. Thank you again!

Leave a Reply