Community Groups are officially here!
We've released Groups, a new feature that enables us to connect community members of similar industries and interests in a shared, private space. You can check out all of the details here, including information about who can join, how to join, and what Groups are currently offered. Please leave your feedback through this Community Groups Feedback Survey.

Using JavaScript with Embedded Data and/or selected choices.

Clint Washington, USACommunity Member, Qualtrics Brand Admin Guru ✭✭

Can someone walk me through pulling Embedded Data and/or selected choices into a JavaScript function? As an example, let's say that I want the sum of numbers from a loop and merge block where it is possible that the user loops through 40 times. Instead of creating 40 different branches in the survey flow each with an equation, hopefully I could just set up a nice little JS function to handle the math.

And the answer is yes, I have done this just because I was too "lazy" to spend the time to figure out and use JS for this survey.

Best Answer

  • AnthonyR
    AnthonyR Tucson AzFounding Community Member, Community Member Superuser ✭✭✭✭
    Accepted Answer


    You can use piped text in your JavaScript, so using something like this will pull answers in just fine:

    var answer = "${e://Field/answer}";

    If that answer embedded data is a number, then you should use parseInt() on it to use it as a number in JavaScript.

    var answer = parseInt(${e://Field/answer});

    Rinse and repeat for anything you need to pull in, using the appropriate piped text.


  • rettweber
    rettweber East LansingCommunity Member, Qualtrics Brand Admin Guru ✭✭

    Hi @AnthonyR,

    That is great info. I searched forever through the Document Object Model to find the embedded data fields.

    Any idea how to set embedded data?

    Also, I assume that we can use the other piped text types to get other data (ie ${q://QID10/ChoiceTextEntryValue} ).


  • rettweber
    rettweber East LansingCommunity Member, Qualtrics Brand Admin Guru ✭✭

    Hi @AnthonyR ,

    I was testing changing the embedded data and I am having issues getting the code to work. Although I can retreive embedded data, Yay! I cannot seem to get a selected choice from a question and I can't seem to set the embedded data value.

    Here is the code I was using ...
    I set test_status = require in survey flow as first element.
    QID5 is a single select dropdown multiple choice question.
    The code is placed in the question Javascript section.

    The code runs when the QID5 option is changed.
    the console shows element1 = empty (actually not sure if null, empty, or space)
    the console shows element2 = require
    the console shows element3 = require

    I am sure I am doing something silly. I can get and change the information using the document object model, but I would prefer to be more direct (ie using Qualtrics functions) as the DOM could change.



    P.S. Sorry I had to include an image of the code. Whenever I tried to include the code I received an access error. I have a call in to Qualtrics support to see what was going on.

  • AnthonyR
    AnthonyR Tucson AzFounding Community Member, Community Member Superuser ✭✭✭✭
    edited December 2017


    Ahh this is a classic case of order of operations.

    Qualtrics populates the piped text on page load, not on execution.
    So essentially your code once the page loads and it executes is:

        document.getElementById("QR~QID5").onChange = function(element){
            var element1 = "";
            var element2 = "require";
            Qualtrics.SurveyEngine.setEmbeddedData('test_status', 'changed value');
            var element3 = "require";
            document.getElementById("QR~QID4").value = element1;

    If you check the embedded data on the next page it will have been updated. And if you want the answers to a question on the same page, you will have to parse through the DOM.

    FYI: when including code, make sure to highlight all of it, and format it as code using the format button at the top of the edit window.

  • nathanbos
    nathanbos MarylandCommunity Member, Qualtrics Brand Admin Qubie ✭

    How do I refer to a choice text in a loop & merge so that it always refers to the current loop?

    I can refer to the first loop's choice as ${q://1_QID88/ChoiceGroup/SelectedChoices}
    And I can refer to the number of the current loop as ${lm://CurrentLoopNumber}

    But nesting the loop number inside the survey question name as below didn't work either in piped text or Javascript


  • TomG
    TomG Raleigh, NCCommunity Member, Qualtrics Brand Admin Wizard ✭✭✭✭✭

    @nathanbos - If you have a new question you should start a new discussion rather than adding to an old one.

    As to your question, refer to the current loop by leaving out the loop number. For example:

  • nathanbos
    nathanbos MarylandCommunity Member, Qualtrics Brand Admin Qubie ✭

    Tom- thank you! I will start a new discussion next time.