Carry Forward Selections from 3 Questions | XM Community
Skip to main content
Question

Carry Forward Selections from 3 Questions

  • February 7, 2024
  • 3 replies
  • 58 views

Forum|alt.badge.img+1

This is my first time ever playing with JavaScript. I am trying to capture through embedded data selected responses from multiple questions to then carry forward to another question. Something like this: 

Q1: select all of your favorite fruits
Q2: select all of your favorite vegetables
Q3: select all of your favorite candy

Q4: [response options are only and all of those previously selected in Q1-Q3] Now rank your favorite foods. 

I’m getting this error: SE API Error:  TypeError: this.hideChoice is not a function. I also don’t seem to be capturing the question selections correctly either. 

Script started
VM1453:9 Selected Q1:  ['']
VM1453:10 Selected Q2:  ['']
VM1453:13 All selected choices:  (2) ['', '']

3 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6084 replies
  • February 7, 2024

@qually,

You would probably be better off combining Q1-Q3 into one question with Choice Groups. Then you wouldn’t need JavaScript. You could just carry forward the selected choices.

The ‘this.hideChoice is not a function error’ means there is no such thing as a function named hideChoice associated with the ‘this’ object.

You would need to post your JS code to get help with the above error or the selections not being captured.


Forum|alt.badge.img+1
  • Author
  • 1 reply
  • February 8, 2024

Thanks so much @TomG ! I think the Choice Groups formatting would not be ideal for the questions I’m asking unfortunately since many of the response options are sort of long. BUT I am very interested in this option for future studies and was not aware of it until now. 

Here’s the code ChatGPT suggested I use: 

Qualtrics.SurveyEngine.addOnload(function() {
    // Debug: Log that the script has started
    console.log("Script started");

    // Retrieve selected choices from Embedded Data and log them
    var selectedQ1 = "${e://Field/SelectedQ1}".split(",");
    var selectedQ2 = "${e://Field/SelectedQ2}".split(",");
    console.log("Selected Q1: ", selectedQ1);
    console.log("Selected Q2: ", selectedQ2);

    var allSelected = selectedQ1.concat(selectedQ2);
    console.log("All selected choices: ", allSelected);

    // Hide all choices initially
    var questionId = this.questionId;
    var choices = this.getChoices();
    choices.forEach((choiceId) => {
        this.hideChoice(choiceId);
    });

    // Attempt to show only the choices that were selected in Q1 and Q2
    allSelected.forEach((selectedChoice) => {
        var choiceNumber = this.getChoiceByText(selectedChoice);
        if (choiceNumber) {
            this.showChoice(choiceNumber);
            console.log("Showing choice: ", selectedChoice); // Log shown choices
        } else {
            console.log("Choice not found for text: ", selectedChoice); // Log if a choice is not found
        }
    });
});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6084 replies
  • February 8, 2024

@qually,

ChatGPT isn’t a reliable source for working code. There are a lot of issues with that code, including at least three functions that don’t exist. There is no simple fix.

You could forego JS by using display logic on the choices.