Setting the value of a question from elsewhere | XM Community
Skip to main content

I am setting up a survey which embeds the respondents previously provided birth year. 

With this, I want to be able to answer a question on behalf of the respondent, which converts that embedded birth year into an approximate age, and assigns that age to a single-response question with age buckets (e.g. 18-24, 25-34, and so on), which a quota logic is tied to. I can achieve that with something like this:

var year = '${e://Field/Birth%20Year}';
    var today = new Date();
    var age = null;
    if (year > 0) {
        age = today.getFullYear() - year - 1;
    }

    var q1 = null;
    if (age >=18 && age<=24) {
        q1 = 1;
    } else if (age>=25 && age<=34) {
        q1 = 2;
    } else if (age>=35 && age<=44) {
        q1 = 3;
    } else if (age>=45 && age<=54) {
        q1 = 4;
    } else if (age>=55 && age<=64) {
        q1 = 5;
    } else if (age>=65) {
        q1 = 6;
    } 

    if (q1!=null) {
        this.setChoiceValueByRecodeValue(q1,true);
    } 
    
    this.clickNextButton();


However, I do not want the respondent to see the question at all. I don’t want it to appear for a second pre-answered, and then go to the next page, which is what this current solution does. 

If I use “Display logic” in the Editor to hide the question from the respondent, the javascript instructions will not execute, and the question remains unanswered.

 

So, how can I set the questions value remotely, as in, from the javascript code of another question? I haven’t been able to find the right way to refer to the question id and set its value remotely. 

I’ve tried things like:

var q1 = '${q://QID1/}'; (and a few different sub-properties I was able to find like /QuestionText)

q1.setChoiceValueByRecodeValue(1,true); 

but this doesn’t do anything.

 

Appreciate any help! :)

Put aside that I can simply set up a quota logic referring to embedded data ranges - is it possible to refer to and set answers to a question with javascript, when another question is active/visible?


Probably too late to help Robert, but for anyone coming across this with the same issue:

 

This won’t set the responses from another question, but thisquestionID.hide will allow the question to be populated from the JS within the Q without the respondent seeing it

 

Qualtrics.SurveyEngine.addOnload(function()
{

    jQuery("#"+this.questionId).hide();
    jQuery("#Buttons").hide();

 

// COMMENT: your code to set the question value and write it to the question would most of the time all go here // 

});

...this snippet of code hides the question and the forward/ next button

Then add the below:

 

Qualtrics.SurveyEngine.addOnReady(function()
{

    jQuery('#NextButton').click();

});

 

...This auto advances to the next question once your JS is executed.

It’s not perfect: if you put several of these in questions back to back you’ll get a bit of “flickering” without showing the question while each (blank) page loads then advances, so avoid that if possible - it just looks weird!, but unless your respondent has a VERY slow browser they won’t experience any disruption on their side while this executes on a single question.


Leave a Reply