Javascript: Default choices on multiple (!) dropdown answers in Matrix Table | XM Community
Skip to main content
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!
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
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.
This is worked great for me - thanks so much for sharing this!

Leave a Reply