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.

Javascript: Default choices on multiple (!) dropdown answers in Matrix Table

G2000
G2000 Community Member, Qualtrics Brand Admin Qubie ✭

Hi all,

I am trying to select default answers based on embedded data. The problem I have is two-fold:

First, and most importantly: I cannot figure out how to select different values for several different drop-down options in the same Matrix Table.
I found the following online, which lets me select the SAME answer on all dropdowns in the same question (in this example, the second from the bottom):
if($('QID17')) {$$('#QID17 select').each(function(s){ s.value = (s[s.length-2].value); }) }
But how do I adapt this to select different answers for the different drop-downs?

Second, my current JavaScript is long, using "IF embeddeddataX = 1 THEN set answer to 1, IF embeddeddataX = 2 THEN set answer to 2, ..."
I am looking for a more elegant way to use the numeric value from embedded data to set the answer choice.

Any help is appreciated!

Tagged:

Best Answers

  • Rich_Boits_Walker
    Rich_Boits_Walker IndianapolisCommunity Member, Qualtrics Brand Admin Sage ✭✭✭
    Accepted Answer

    I've used the setChoiceValueByRecodeValue method documented in the link below. The documentation is describing it's use for a single select, but it can be tweaked to work with a matrix table by passing row in. This works for all four answer types in the matrix table (single, multiple, dropdown, and drag & drop).

    setChoiceValueByRecodeValue(row,value,'true')

    https://s.qualtrics.com/WRAPI/QuestionAPI/classes/Qualtrics%20JavaScript%20Question%20API.html#method_setChoiceValueByRecodeValue

  • Rich_Boits_Walker
    Rich_Boits_Walker IndianapolisCommunity Member, Qualtrics Brand Admin Sage ✭✭✭
    Accepted Answer

    Here is a simple example of what I mean using embedded data fields statement1 and statement2 that are set before the matrix question block.

    Qualtrics.SurveyEngine.addOnload(function()
    {
        var selectedPunch1 = parseInt("${e://Field/statement1}");
        var selectedPunch2 = parseInt("${e://Field/statement2}");
    
        // matrix table arguements are (row,value,'true')
        // selects punch in a matrix table (row_#_in_matrix_table,punch_from_var,'true')
        this.setChoiceValueByRecodeValue(1,selectedPunch1,'true');
        this.setChoiceValueByRecodeValue(2,selectedPunch2,'true');
    });
    

    Note that I added parseInt around the embedded data to workaround the piped text call's double slash which is a comment in javascript.

Answers

  • G2000
    G2000 Community Member, Qualtrics Brand Admin Qubie ✭

    This is worked great for me - thanks so much for sharing this!