Set side-by-side default using Javascript | XM Community
Skip to main content

I have a side-by-side question with rows of Athlete1, Athlete2, Athlete3 etc. I have columns ranging from Sunday to Saturday. In each cell, I have a dropdown menu with 8 items.

What I want to do is allow someone an “Apply to all” option for each day, so that the dropdown menu defaults to that value. The person would then only need to alter a small number of cells as needed.

My thought was to have a first side-by-side question that the default values could be selected for each day. Then, when moving on to the actual side-by-side question, this can be pre-populated with the responses to that first question. The javascript I wrote for the second question is below but it is not working. If anyone can provide guidance on the code or another way this could be accomplished I would be very grateful!

 

Qualtrics.SurveyEngine.addOnload(function() {
 
    var triggerQuestionId = 'QID3';

    // Get the responses from 'Exposure Defaults' for each day
    var sundayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_1', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_1'));
    var mondayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_2', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_2'));
    var tuesdayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_3', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_3'));
    var wednesdayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_4', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_4'));
    var thursdayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_5', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_5'));
    var fridayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_6', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_6'));
    var saturdayResponse = Qualtrics.SurveyEngine.getChoiceRecodeValue(triggerQuestionId + '_7', Qualtrics.SurveyEngine.getChoiceAnswerValue(triggerQuestionId + '_7'));

    var sideBySideQuestionId = 'QID1';

    // Set defaults for Athlete1 to Athlete8 based on the selected options
    for (var i = 1; i <= 8; i++) {
        var athleteId = sideBySideQuestionId + '_row' + i;
        var sundayInputId = athleteId + '_2'; 
        var mondayInputId = athleteId + '_3';
        var tuesdayInputId = athleteId + '_4';
        var wednesdayInputId = athleteId + '_5';
        var thursdayInputId = athleteId + '_6';
        var fridayInputId = athleteId + '_7';
        var saturdayInputId = athleteId + '_8';

        // Set default values based on the selected options
        jQuery('#' + sundayInputId + ' input').val(sundayResponse);
        jQuery('#' + mondayInputId + ' input').val(mondayResponse);
        jQuery('#' + tuesdayInputId + ' input').val(tuesdayResponse);
        jQuery('#' + wednesdayInputId + ' input').val(wednesdayResponse);
        jQuery('#' + thursdayInputId + ' input').val(thursdayResponse);
        jQuery('#' + fridayInputId + ' input').val(fridayResponse);
        jQuery('#' + saturdayInputId + ' input').val(saturdayResponse);
    }
});

Be the first to reply!

Leave a Reply