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?


Leave a Reply