Drill down question JavaScript | XM Community
Skip to main content

Hi @TomG,

 

I’m currently trying to code a survey where I’m asking the following drill-down questions:

In the first question I ask for the expected change in Overall spends followed by Tactical and Strategic. The “Overall” spends decide what options will be shown in the “Tactical” and “Strategic” rows (I need to keep these two dependent on overall spends).

In the second question I ask for the expected change in Overall spends (again) followed by expected change in spends in North America and Europe. Here as well, “Overall” will decide what options will be shown in “North America” and “Europe” (I need to keep these two dependent on overall spends).

 

Because the first two rows (Overall) spends of the two questions are the same, I want them to be equal and hence I am using the following JavaScript code to check the Overall spends asked in Q92 against the overall spends asked in Q93. If the two spends are different I want Q93 to either reload or at least prevent the respondent from moving to the next page from Q93.

Code:    

var $jq = jQuery.noConflict();
    var spends_prev= "${q://QID187/ChoiceGroup/SelectedAnswers/1}"
    var spends_current = "${q://QID188/ChoiceGroup/SelectedAnswers/1}"
    if (spends_prev != spends_current)
    {
            $jq('.ValidationError').eq(1).show().html("Cannot select together");
        $jq('#NextButton').prop('disabled',true);
    }

 

The error: 

 

Could please help in resolving this code as it is showing an error when I try to run it or suggest an alternative way of asking the question. 

@K_garg - It isn’t going to work that way. You’re trying to disable the next button after it has already been clicked. 

I think a better approach would be to add JS to automatically select the value in QID188 to match the value selected in QID187 and disable the select in addOnload().

I’m not sure where the error is coming from since your JS doesn’t call getQuestionInfo. It may be from some previous question.


Hi @TomG, understood the explanation,

I was trying to set a default choice here by piping an option the respondent selected in a previous question using a solution you had shared in the following discussion: 

 

However the code is not showing the desired answer. Could you please have a look and correct my mistake, it would be really really helpful!!.

Here is the code I’m using for reference:


var $jq = jQuery.noConflict();
var selects = $jq("#"+this.questionId+" select");
selects.eq(0).val("${q://QID179/ChoiceGroup/SelectedAnswers/1}"); //Set first select value to 2
console.log("${q://QID179/ChoiceGroup/SelectedAnswers/1}")
 

Screenshots of the question:
 


Screenshot of the code (I’ve tried placing it in both the functions “AddonReady” and “AddonLoad” but it has not worked in either):
 


 

On previewing this is how it still looks even after adding the code:

 

Also to give more context these are the answer options I’m providing here (same answer options have been provided in the previous question (QID179) from where I’m piping the response).

 

Any help would be really amazing. 


@K_garg,

The select values are numbers and what you are piping is the labels.  Choice ids (values) in drill downs are tricky because each unique combination is assigned an id (i.e., the ids in the first select aren’t 1,2,3,...)  You’ll need to search the option labels for a match, then set the select value to be the the matching option id.


Leave a Reply