Java script - reference previous answers | XM Community
Skip to main content

Hi, I’m trying to create a text entry question called Risk Rating that will calculate one’s eligibility based on their answers to prior questions in the survey. I’ve written this Java Script but nothing seems to appear in the actual text entry box for Risk Rating when I preview the survey. If anyone has any assistance or recommendations that would be greatly appreciated. Thanks!

 

Qualtrics.SurveyEngine.addOnload(function() {
    // Function to check eligibility based on the answers
    function checkEligibility() {
        var suspendedAnswer = "${q://QID4/ChoiceGroup/SelectedChoices}";
        var delinquentAnswer = "${q://QID5/ChoiceGroup/SelectedChoices}";
        var singleAuditAnswer = "${q://QID6/ChoiceGroup/SelectedChoices}";
        var ueAnswer = "${q://QID7/ChoiceGroup/SelectedChoices}";

        if (suspendedAnswer === "No" ||
            delinquentAnswer === "No" ||
            singleAuditAnswer === "No" ||
            ueAnswer === "No") {
            // Set the eligibility to "Ineligible"
            Qualtrics.SurveyEngine.setEmbeddedData("Eligibility", "Ineligible");
        } else {
            // Set the eligibility to "Eligible"
            Qualtrics.SurveyEngine.setEmbeddedData("Eligibility", "Eligible");
        }
    }

    // Add event listeners to check for changes in the question answers
    var questionIds = n"QID4", "QID5", "QID6", "QID7"];

    for (var i = 0; i < questionIds.length; i++) {
        var qID = questionIdsi];
        this.questionclick = checkEligibility;
    }

    // Initial check
    checkEligibility();
});

@sam.s77 Is there a reason why you do not set the embedded data in a branch after the questions were asked?

When do you need embedded data “Eligibility”?

And where have you added the JavaScript?


@sam.s77,

You have a few issues.

First, the piped answers (e.g., "${q://QID4/ChoiceGroup/SelectedChoices}") will never update. The pipes are resolved before the page is sent to the browser.  If you want to get those answers dynamically, you need to get them directly from the DOM or from the question’s object.

Second, your click handler only applies to the question the JS is attached to (i.e., ‘this’). You’ll need a completely different approach to listen for changes to the questions.

Your JS would work (set the value ‘Eligibility’) if it is on a page after the previous questions (QID4 - QID6) , but you won’t be able to use ‘Eligibility’ until the page following your JS.


@chackbusch  

  This is my updated survey flow. I need the embedded data “Eligibility” at the end of my survey and want to display that data in the Risk score and rating block. And the Java Script was added directly onto my Risk Rating question. within the Risk score and rating block. 

 


@chackbusch  

This is my updated survey flow. I need the embedded data “Eligibility” at the end of my survey and want to display that data in the Risk score and rating block. And the Java Script was added directly onto my Risk Rating question. within the Risk score and rating block. 

 

 

 


@sam.s77 Can you show the question in the risk score and rating block which should show the value of the embedded data? 

As you have added the branches and defined the embedded data in the survey flow, I do not see any need to have the JavaScript as this is basically doing the same as your branches do. 

Probably you have not correctly piped the “Eligibility” embedded data in your risk score and rating block. 


@chackbusch Is this correct for Risk Rating? I have not yet written how to calculate Risk Score.

 


@sam.s77 Theoretically, it is correct. However, add it via default choices to the text entry. 

Will then look just as in your screenshot, however, the default choice icon should be visible. 

Best
Christian


@chackbusch Got it, thank you so much!


@sam.s77 Great. Would be nice if you could accept my response as solution. 🙂


Leave a Reply